Skip to content
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
84 changes: 83 additions & 1 deletion 02-nodejs/authenticationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,91 @@
Testing the server - run `npm run test-authenticationServer` command in terminal
*/

const express = require("express")
const express = require("express");
const PORT = 3000;
const app = express();
// write your logic here, DONT WRITE app.listen(3000) when you're running tests, the tests will automatically start the server
const bodyParser = require("body-parser");

app.use(bodyParser.json());

const users = [];

app.post("/signup", handleSignUp);
app.post("/login", handleLogin);
app.get("/data", isAuthenticated, getAllUsers);
app.use((_, res) => {
res.status(404).send("Route not found");
});

function handleSignUp(req, res) {
const { email, password, firstName, lastName } = req.body;

if (users.length > 0) {
const foundUserIndex = users.findIndex((user) => user.email === email);
if (foundUserIndex > -1) res.status(400).send("Email already exists");
}

users.push({
email,
password,
firstName,
lastName,
});

res.status(201).send("Signup successful");
}

function handleLogin(req, res) {
const { email, password } = req.body;

const foundUser = users.find(
(user) => user.email === email && user.password === password
);
if (!foundUser) res.status(401).send("Invalid Credentials");

res.json({
email: foundUser.email,
firstName: foundUser.firstName,
lastName: foundUser.lastName,
authToken: generateToken(24),
});
}

function generateToken(length) {
const charset =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let token = "";

for (let i = 0; i < length; i++) {
const index = Math.floor(Math.random() * charset.length);
token += charset[index];
}

return token;
}

function isAuthenticated(req, res, next) {
const { email, password } = req.headers;

const foundUserIndex = users.findIndex(
(user) => user.email === email && user.password === password
);

if (foundUserIndex > -1) next();
else res.status(401).send("Unauthorized");
}

function getAllUsers(req, res) {
const formattedUsers = users.map((user) => {
return {
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
};
});

res.json({ users: formattedUsers });
}

module.exports = app;
51 changes: 48 additions & 3 deletions 02-nodejs/fileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,55 @@

Testing the server - run `npm run test-fileServer` command in terminal
*/
const express = require('express');
const fs = require('fs');
const path = require('path');
const express = require("express");
const fs = require("fs");
const path = require("path");
const app = express();

const DIR_PATH = path.join(__dirname, "files");

app.get("/files", getFiles);
app.get("/file/:filename", getFileByName);
app.use((_, res) => {
res.status(404).send("Route not found");
});

function getFiles(req, res) {
return fs.readdir(DIR_PATH, (err, files) => {
if (err) throw err;

res.json(files);
});
}

function getFileByName(req, res) {
const fileName = req.params.filename;

if (!fileName) res.status(400).send({ error: "No file name specified" });

return fs.readdir(DIR_PATH, (err, files) => {
if (err) throw err;

const isFileFound = files.indexOf(fileName) > -1;

if (isFileFound) {
readFromFile(path.join(DIR_PATH, fileName), (err, data) => {
if (err) throw err;

res.send(data);
});
} else {
res.status(404).send("File not found");
}
});
}

function readFromFile(filePath, cb) {
return fs.readFile(filePath, "utf8", (err, data) => {
if (err) return cb(err);

return cb(null, data);
});
}

module.exports = app;
100 changes: 84 additions & 16 deletions 02-nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions 02-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"testTimeout": 10000
},
"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.18.2",
"uuid": "^9.0.0"
}
Expand Down
Empty file added 02-nodejs/store.json
Empty file.
Loading