Management Section in Node.js – Java Code Geeks


Hello. This study will focus on component management in the function of node.js.

1. Introduction

Phase control is an important part of any user experience that enables the app to track users and their activities. To understand this we will be using a cookie that contains the key keys that are stored in the browser. The browser then sets cookies for each HTTP request sent to the server.

1.1 Setting up Node.js

Installation Policy on windows you need to download installer from this sleep. Click on the builder (see also the NPM package manager) on your platform and run the installer to start with the Node configuration wizard. Follow the wizard’s path and click to finish when finished. If all goes well you can go to the call center to see if the installation is successful as shown in Figure 1.

Fig. 1: Check node configuration with npm

2. Editing a Section in Node.js

In order to set up the program, we need to walk the path in which our work will be. On the software side, which I use Code Visual Studio like my favorite IDE. You are free to choose the IDE of your choice.

2.1 Implementation implementation

Let’s list the various files that might be needed for the study.

2.1.1 Establish dependency

Navigate to the project folder and run it npm init -y create a file of package.json file. These file contains the necessary metadata for the project and is used to monitor project dependence, documentation, type, and much more. Add the following number to the file that describes the requirements that may be required.

phukusi.json

{
  "name": "session-management-app",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "start": "nodemon server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cookie-parser": "^1.4.5",
    "express": "^4.17.1",
    "express-session": "^1.17.2"
  },
  "devDependencies": {
    "dotenv": "^10.0.0",
    "nodemon": "^2.0.12"
  }
}

Deprecation of dependency drives the process that the file contains and uses npm install law. If all goes well, the cores are stored inside node_modules folder and you are better off going with other options.

2.1.2 Creating directors

In the root folder add the following to the index file. This file will be responsible for initiating import and export processes. Remember to create a file of .env file in the same location and explain the privacy information, such as the password, username, and more.

server.js

if (process.env.NODE_ENV !== "production") {
  require("dotenv").config();
}

// imports
const express = require("express");
const session = require("express-session");
const cookieParser = require("cookie-parser");

const app = express();
app.use(cookieParser());
// session configuration
app.use(
  session({
    secret: process.env.SESSION_SECRET || "8unto0n4oc7903zm",
    resave: false,
    saveUninitialized: false,
  })
);

const appUsr = {};

// routes

// http post - http://localhost:7501/login?name=daniel
app.post("/login", (req, res) => {
  appUsr.name = req.query.name;

  // some business logic here ...
  req.session.user = appUsr;
  req.session.save();
  return res.status(201).json({ info: "User logged in" });
});

// http get - http://localhost:7501/user
app.get("/user", (req, res) => {
  if (req.session.user != null) {
    return res
      .status(200)
      .json({ info: "User profile info", data: req.session.user });
  } else {
    return res.status(200).json({ info: "No data present in session" });
  }
});

// http post - http://localhost:7501/logout
app.post("/logout", (req, res) => {
  req.session.destroy();
  return res.status(204).json({ info: "User logged out" });
});

// start server
const port = process.env.APPLICATION_PORT || 7501;
app.listen(port, () => {
  console.log("Server listening at http://localhost:%s", port);
});

3. Run the Work

To get started using this software go to the project folder and enter the following command as shown in fig. 2. If all goes well the app will launch properly on the port number read from .env file or 7501.

Node.js section - launch the app
Fig. 2: Starting work

4. Demonstration

You are free to use mad or any other tool you would like for the HTTP request to end.

Conclusion

// http post - http://localhost:7501/login?name=daniel
// http get - http://localhost:7501/user
// http post - http://localhost:7501/logout

This is about the topic and I hope this article has helped you with anything you are looking for. Fun Learning and don’t forget to share!

5. Summary

In this study, we learned about component management in the function of nodejs. You can download the code and postman source from Downloads section.

6. Download Function

This was a study to manage component management in the work of nodejs.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *