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
83 changes: 83 additions & 0 deletions 02-nodejs/authenticationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,91 @@
*/

const express = require("express")
var bodyParser = require('body-parser');

const PORT = 3000;
const app = express();

app.use(bodyParser.json());

const users = [];

app.post('/signup', (req, res) => {

if (users.length > 0) {

const filteredUsers = users.filter(item => item.username === req.body.username);

if (filteredUsers.length > 0) {
res.status(401).send("Bad Request")
}
else {
users.push({
id: users.length,
username: req.body.username,
password: req.body.password,
firstName: req.body.firstName,
lastName: req.body.lastName
});

res.status(201).send("Created")
}
}
else {
users.push({
id: users.length,
username: req.body.username,
password: req.body.password,
firstName: req.body.firstName,
lastName: req.body.lastName
});

res.status(201).send("Created")
}
});

app.post("/login", (req, res) => {

const { username, password } = req.body;

const loggedInUser = users.filter(item => item.username === username && item.password === password);

if (loggedInUser.length > 0) {
res.status(200).json({
token: loggedInUser?.[0].id
});
}
else {
res.status(401).send("Unauthorized");
}
});

app.get("/data", (req, res) => {

const { username, password } = req.headers;

const filteredUsers = users.filter(item => item.username === username && item.password === password);

if (filteredUsers.length > 0) {
res.status(200).json({
users: filteredUsers.map(item => ({
firstName: item.firstName,
lastName: item.lastName,
id: item.id
}))
});
}
else {
res.status(401).send("Unauthorized");
}

});

app.use((req, res) => {
res.status(404).send("404 Not Found!");
});

// write your logic here, DONT WRITE app.listen(3000) when you're running tests, the tests will automatically start the server
app.listen(PORT);

module.exports = app;
45 changes: 45 additions & 0 deletions 02-nodejs/fileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,50 @@ const fs = require('fs');
const path = require('path');
const app = express();

app.get("/files", (req, res) => {

const reqPath = req.path;

const dirPath = path.join(__dirname, reqPath);

fs.readdir(dirPath, (err, files) => {

if (err) {
res.status(401).send("unauthorized");
}
else {
if (files.length > 0) {
res.status(200).json({
files
});
}
else {
res.status(401).send("files Not Exist");
}
}
});
});

app.get('/files/:fileName', (req, res) => {

const reqPath = req.path;

const filePath = path.join(__dirname, reqPath);

fs.readFile(filePath, 'utf-8', (err, data) => {
if (err) {
res.status(400).send("File not found");
}
else {
res.status(200).send(data);
}
});
});

app.all("*", (req, res) => {
res.status(404).send("Route Not Found!");
});

app.listen(3000);

module.exports = app;
Loading