Skip to content

Feat: Create a dummy backend #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.development.local.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
VITE_API_BASE_URL=REPLACE
VITE_API_BASE_PATH=REPLACE
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ plugins:
- "check-file"
ignorePatterns:
- build/**/*
- backend-dummy/**/*
rules:
# Eslint Possible Problems
array-callback-return: ["error", { allowImplicit: true }]
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ You'll have to link it to the project remote manually.
npm install
```

5. Delete the dummy backend folder:

```shell
rm -r backend-dummy
```

And that's it! now you are ready to build on top, the commands to start, test and run storybook are listed above.

## Project structure
Expand Down
37 changes: 37 additions & 0 deletions backend-dummy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Dummy backend

This backend is a lightweight, Express-based application designed to facilitate rapid testing of common workflows. It serves as a sandbox for developers to validate front-end integrations, prototype new features without relying on fully-fledged backend systems.

## Setup

In the backend-dummy folder run the following commands:

1. Install the required packages:

```shell
npm install
```

2. Starting the backend

```shell
npm start
```

## How it works?

This backend does not use a real database. However, there is a file named users.json that acts as a database. Refer to this file to see the available users for testing purposes.

## Endpoints

- [POST] users/login

**Description**

Validates the provided email and password. If successful, it sets a cookie with the user's session ID.

**Responses**

- **200 OK:** Login successful. The user's session cookie is set.
- **400 Bad Request:** The request body is missing required fields (email or password).
- **401 Unauthorized:** The provided email or password does not match any existing user.
18 changes: 18 additions & 0 deletions backend-dummy/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");

var usersRouter = require("./routes/users");

var app = express();

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

app.use("/api/users", usersRouter);

module.exports = app;
86 changes: 86 additions & 0 deletions backend-dummy/bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require("../app");
var debug = require("debug")("backend-dummy:server");
var http = require("http");

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || "3000");
app.set("port", port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on("error", onError);
server.on("listening", onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== "listen") {
throw error;
}

var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debug("Listening on " + bind);
}
Loading