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
82 changes: 76 additions & 6 deletions 02-nodejs/authenticationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,79 @@
Testing the server - run `npm run test-authenticationServer` command in terminal
*/

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

module.exports = app;
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

var users = [];

app.use(express.json());
app.post("/signup", (req, res) => {
var user = req.body;
let userAlreadyExists = false;
for (var i = 0; i<users.length; i++) {
if (users[i].email === user.email) {
userAlreadyExists = true;
break;
}
}
if (userAlreadyExists) {
res.sendStatus(400);
} else {
users.push(user);
res.status(201).send("Signup successful");
}
});

app.post("/login", (req, res) => {
var user = req.body;
let userFound = null;
for (var i = 0; i<users.length; i++) {
if (users[i].email === user.email && users[i].password === user.password) {
userFound = users[i];
break;
}
}

if (userFound) {
res.json({
firstName: userFound.firstName,
lastName: userFound.lastName,
email: userFound.email
});
} else {
res.sendStatus(401);
}
});

app.get("/data", (req, res) => {
var email = req.headers.email;
var password = req.headers.password;
let userFound = false;
for (var i = 0; i<users.length; i++) {
if (users[i].email === email && users[i].password === password) {
userFound = true;
break;
}
}

if (userFound) {
let usersToReturn = [];
for (let i = 0; i<users.length; i++) {
usersToReturn.push({
firstName: users[i].firstName,
lastName: users[i].lastName,
email: users[i].email
});
}
res.json({
users
});
} else {
res.sendStatus(401);
}
});

module.exports = app;

52 changes: 46 additions & 6 deletions 02-nodejs/fileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,50 @@

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

app.use(express.json());

const listOfFiles = async (req, res) => {
try {
const files = await fs.readdir('./files');
res.status(200).json(files);
}
catch (error) {
res.sendStatus(500);
}
};

const getFileContent = async (req, res) => {
const { filename } = req.params;
const filePath = `./files/${filename}`;

try {
const stats = await fs.stat(filePath);

if (!stats.isFile()) {
res.status(404).send("File not found");
return;
}

const fileContent = await fs.readFile(filePath, 'utf8');
res.status(200).send(fileContent);
}
catch (error) {


module.exports = app;
res.status(404).send("File not found");
}
};

app.get('/files', listOfFiles);
app.get('/file/:filename', getFileContent);

app.use((req, res) => {
res.status(404).send("Route not found");
});

module.exports = app;

Loading