You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
3.9 KiB
JavaScript
146 lines
3.9 KiB
JavaScript
/*
|
|
Introduction to Express.js
|
|
*/
|
|
|
|
// import express module
|
|
const express = require('express');
|
|
|
|
// Create an application using express
|
|
const app = express();
|
|
|
|
// Port
|
|
const port = 4000;
|
|
|
|
// Middlewares
|
|
/*
|
|
- Middleware is a software that provides common services and capabilities to application outside of what is offered by the system/
|
|
- API management is one of the common application of middlewares
|
|
*/
|
|
// Allows our app to read JSON data
|
|
app.use(express.json());
|
|
// Allows our app to read data from forms and extend the data types that our app will support
|
|
app.use(express.urlencoded({extended: true}));
|
|
|
|
|
|
// [Section] Routes
|
|
|
|
/*
|
|
- Express has methods corresponding to each HTTP method
|
|
- This route expects to receive a GET request at the base URI "/"
|
|
*/
|
|
app.get("/", (req, res) => {
|
|
|
|
// Once the route is accessed, it will send a string response containing "Hello world"
|
|
// we use the res.send() method to send a response back to the client
|
|
res.send("Hello world");
|
|
});
|
|
|
|
// route for accessing the "/hello" with a GET method
|
|
app.get("/hello", (req, res) => {
|
|
res.send("Hello from the /hello endpoint!");
|
|
});
|
|
|
|
// This route expects to receive a POST request at the URI "/hello"
|
|
app.post("/hello", (req, res) => {
|
|
|
|
// req.body contains the contents/data of the request body
|
|
// All properties defined in our Postman request will be accessible here as properties with the same name
|
|
res.send(`Hello there ${req.body.firstName} ${req.body.lastName}`);
|
|
});
|
|
|
|
// This will serve as our mock database
|
|
let users = [];
|
|
|
|
// This route expects to receive a POST request at the URI "/signup"
|
|
app.post("/signup", (req, res) => {
|
|
console.log(req.body);
|
|
|
|
// if the contents of the request body with the property "username" and "password" is not empty (empty string)
|
|
if(req.body.username !== '' && req.body.password !== ''){
|
|
|
|
// This will store the user object sent via Postman to the users array created above
|
|
users.push(req.body);
|
|
|
|
console.log(users);
|
|
|
|
// This will send a response back to the cliet/Postman
|
|
res.send(`User ${req.body.username} successfully registered`);
|
|
|
|
// If the username and password are not complete, an error message will be sent back to the client/Postman
|
|
} else {
|
|
|
|
res.send("Please input BOTH username and password");
|
|
}
|
|
});
|
|
|
|
// This route expects to receive a PUT request at the URI "/change-password"
|
|
// This will update the password of the user
|
|
app.put("/change-password", (req, res) => {
|
|
|
|
let message;
|
|
|
|
// Creates a for loop that will loop through the elements of the "users" array
|
|
for(let i = 0; i < users.length; i++){
|
|
|
|
// If the username provided by the client/Post and the username of the current user is the same
|
|
if(req.body.username == users[i].username){
|
|
|
|
// Change the password by reassignment
|
|
users[i].password = req.body.password
|
|
|
|
// reassign the value of the message to be sent back to the client/Postman
|
|
message = `User ${req.body.username}'s password has been updated.`;
|
|
|
|
console.log(users);
|
|
|
|
// stop the loop
|
|
break
|
|
|
|
// If no user was found
|
|
} else {
|
|
message = "User does not exist.";
|
|
}
|
|
}
|
|
|
|
res.send(message);
|
|
});
|
|
|
|
app.get('/home', (req,res) => {
|
|
|
|
res.send('Welcome to the homepage');
|
|
});
|
|
|
|
app.get('/users',(req,res) => {
|
|
|
|
res.send([{username:'john',password:'1234' }]);
|
|
});
|
|
|
|
app.delete("/delete-user", (req, res) => {
|
|
|
|
let message;
|
|
|
|
// Creates a for loop that will loop through the elements of the "users" array
|
|
for(let i = 0; i < users.length; i++){
|
|
|
|
// If the username provided by the client/Post and the username of the current user is the same
|
|
if(req.body.username == users[i].username){
|
|
usernames.splice(users[i].username, 1);
|
|
|
|
message = `User ${req.body.username} has been deleted.`;
|
|
|
|
console.log(users);
|
|
|
|
// stop the loop
|
|
break
|
|
}
|
|
|
|
res.send(message);
|
|
};
|
|
|
|
|
|
// An if statement is added here to be able to export the following app/server for checking
|
|
if(require.main === module){
|
|
app.listen(port, () => console.log(`Server running at port ${port}`));
|
|
}
|
|
|
|
module.exports = app; |