NodeJs1: Server request/responses with Postman, CORS, and JWT authentification
NodeJs is a server-side javascript environment for app development.
We use ExpressJs, an open-source NodeJs framework, to handle HTTPS requests with routing support.
The REST (Representational State Transfer) API exchanges data between applications through HTTPS methods like GET, PUT, POST, and DELETE. (Client-> API -> Database -> Database data).
To start a NodeJs server we import/require express methods, each route will include an endpoint and its handler function:
- npm Init //We start up a package.json file
//The endpoint sets where the request and response is gonna take place
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send("Hello World!");
});
//listen() sets the localhost: endpoint
app.listen(3000, () => console.log("Server is up and running"))
//node server.js to start the server
The endpoint is the part of the URL that comes after /.
Contrary to React, the server needs to re-start in order to update, to avoid that we npm i nodemon.
//In the package.json we create a custom script
"scripts": {
"start": "nodemon server.js" //now is npm start
}
For more information on using fetch in client-server request/response interactions, see this link.
Postman is a scalable testing tool, it can retrieve information sent by the server routes.
Queries are the url part that comes after a ?...=, it is used to pass information from the endpoint to the server routes.
//We request them with req.query.___, and add them to the endpoint
//we add a query with ?(name set in route)=(query value) in the URL
app.get('/', (req, res) => {
let cava = req.query.v
res.send("Hello World! , we have the " + cava );
});

Parameters are properties attached to the URL, prefixed with (:) on the endpoint, and requested with req.params.___.
//http://localhost:3000/add1/12/74
//we pass its multiple values in the Endpoint
app.get("/add1/:primo/:second", function (req, res) {
let sum1 = Number( req.params.primo )
let sum2 = Number( req.params.second )
res.send("The result is " + (sum1 + sum2 ));
});
Postman allows testing of custom headers for use in server.js.
//We can use Auth bearer token or custom headers
function authenticate(req, res, next) {
let token = req.header("Authorization");
console.log("Postman tested token " + token); //
}
router.post("/sign-up", async (req, res) => {
const {name, email, password} = req.body;
let token = req.header("token");
console.log( token )
}

For more information about Middlewares check here.
Modify a JSON file with Postman POST data
To body-parse request body elements we use() the express.json() built-in middleware. We install file-system (fs) which will allow us to update server files.
//Unlike normal middleware, it doesn't need to be included in routes
//This allows request.body to be available in route paths.
app.use(express.json())
npm i file-system
const fs = require("fs")
We request the body from Postman Post and update the imported JSON array using fs.
//we set the new object ID to be the last of the JSON.
const quotes = require("./quotes.json"); //[{}, {}, {}, ...]
app.post("/quotes", function(req, res){
const corpo = req.body
corpo.id = quotes.length
quotes.push( corpo )
fs.writeFileSync("./quotes.json", JSON.stringify(quotes));
res.send( "Pushed one" )
})

Both Post and Put Postman methods can update and create elements, Put is idempotent, its results remain the same not matter how many times it's repeated.
CORS and JWT autentification
CORS, Cross-Origin Resource Sharing, allows servers to set specific origins for their request. Origins are defined by their protocol HTTP and port number.
HTTP is a stateless protocol, it won't record any request data, so to authenticate the user and to share data between the browser and server we used sessions.
Session objects track users by comparing their cookies (string files downloaded on website access) to the ID session. When users make a request (log in) it checks if they have an already open session and what permits they have.
Sessions need storage space and extra security when sent to the server, they make the app harder to scale, and it's hard to implement on apps that contain many back-end micro-services or don't use the browser for their cookies.

The JSONWebToken (JWT) registers the user directly to the app without any sessions. JSON stands for Javascript Object Notation, a text-based data format transferable between all languages and standard syntax for APIs.
The JWT is made of clains (string sections) separated by a comma, clains are encoded in code-64.
The first header clain contains the hashing algorithm and the token type. The second contains the JSON object sent to the user, visible to anyone. The third is a secret hash, kept by the server and it resets if the original request changes.

Implementing JWT registration to the server
The JWT user server will have this structure.
/server
server.js
/utils
generateJWT.js
auth.js
/routes
user.js
Check the CORS section to know more about the configuration.
//Implement the CORS middleware
const express = require("express");
const app = express();
app.use(express.json()); //req.body parser
const cors = require("cors");
const corsOptions = {
origin: "http://localhost:3000"
};
app.use(cors(corsOptions));
const user = require("./routes/user.js");
app.use("/user", user);
The /user route will handle all user server calls, check the Express routing section for more.

We set the utilities following the JWT section
We store the JWT key in a safe place, like an env file.
//Exported and used as middleware in /user
const jwt = require("jsonwebtoken");
require("dotenv").config();
function generateJWT(user_id) {
const payload = {
user: {
id: user_id
}
};
return jwt.sign(payload, process.env.jwtSecret, { expiresIn: "1h" });
}
module.exports = generateJWT;

Last updated
Was this helpful?