Home > Web Front-end > Front-end Q&A > nodejs backend deployment test interface

nodejs backend deployment test interface

王林
Release: 2023-05-08 10:45:36
Original
668 people have browsed it

With the development of network technology, the demand for Web applications has gradually increased, and back-end development methods have also become diversified. As a back-end technology based on event-driven, non-blocking I/O, Node.js has become the back-end language chosen by more and more developers due to its excellent performance and scalability.

But, when we complete the development of the Node.js backend, how to deploy and test it? Today we will discuss the deployment and testing of Node.js backend.

Deployment

The deployment of Node.js applications is mainly divided into two stages-production environment and development environment. The deployment of the development environment is mainly to facilitate testing, while the deployment of the production environment is to ensure the stability and reliability of the application.

  1. Development environment deployment

In the development environment, the HTTP server module that comes with Node.js is generally used to start the program to facilitate testing and debugging. The specific steps are as follows:

Step 1: Install the Node.js environment

First, you need to install the Node.js environment. You can download the latest version of Node.js from the Node.js official website.

Step 2: Create a Node.js project

Use the command line to create a folder in any directory, and then execute the following command in the directory:

# 初始化项目
npm init

# 添加Express框架
npm install express --save

# 添加nodemon 自动重启应用程序
npm install nodemon --save-dev
Copy after login

Complete execution After the above command, we can start writing our Node.js program.

Step 3: Write the Node.js program

Take the Express framework as an example, create a routing module:

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
  res.send('Hello World!');
});

module.exports = router;
Copy after login

Step 4: Start the Node.js program

We can use the command line to execute the following command to start our program:

# 启动程序
node app.js
Copy after login

Then, we can access http://localhost:3000/ in the browser and see The program outputs Hello World!.

At this time, we need to manually restart the server every time we modify the code, which is troublesome, so we can use nodemon to automatically restart the server.

First add the following configuration to the package.json file:

{
  "scripts": {
    "start": "nodemon app.js"
  }
}
Copy after login

Then, we can use the following command to start the server:

# 启动程序
npm start
Copy after login

This way , after modifying the code, we only need to save the code file, and the program will automatically restart and apply the modifications.

  1. Production environment deployment

In the production environment, in order to ensure the stability and reliability of the application, we need to use a server dedicated to deployment, such as Nginx, Apache etc.

Here, we take Nginx as an example to introduce how to deploy Node.js applications.

Step 1: Install Nginx

First you need to install Nginx on the server. You can use the following command to install:

sudo apt-get update
sudo apt-get install nginx
Copy after login

Step 2: Configure Nginx

You need to add a new virtual host to the Nginx configuration file. The configuration file is generally located at /etc/nginx/sites-available/default.

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
Copy after login

In the above configuration, proxy_pass specifies the address of the Node.js program forwarded to: http://127.0.0.1:3000, server_name specifies the domain name corresponding to the virtual host.

It should be noted that the address in proxy_pass should be the listening address of the Node.js program. If the Node.js program does not listen on port 3000, it needs to be modified accordingly.

Step 3: Restart Nginx

After completing the configuration, you need to restart Nginx to apply the configuration. You can use the following command:

sudo systemctl restart nginx
Copy after login

At this point, our Node.js application has been successfully deployed to the production environment.

Testing

After completing the deployment, we also need to conduct interface testing to ensure the stability and reliability of the application.

Here, we use Postman for interface testing.

Step 1: Install Postman

You can go to the Postman official website to download the latest version of Postman.

Step 2: Set up the test environment

In Postman we can set up the global environment to facilitate testing.

We can select the "Environment Variables" option in the upper left corner of Postman and add environment variables there:

{
    "url": "http://localhost:3000"
}
Copy after login

Among them, url points to the Node.js program we deployed the address of.

Step 3: Write a test case

Create a test case:

{
    "name": "测试Hello接口",
    "request": {
        "method": "GET",
        "header": [],
        "url": {
            "raw": "{{url}}/",
            "host": [
                "{{url}}"
            ]
        }
    },
    "response": []
}
Copy after login

In the above test case, we set the request method and request address, where{ {url}} is url in the global environment variable.

Step 4: Test case execution

After setting up the test case, we can click the "Send" button to execute the test case and get the interface request result.

Summary

The deployment and testing of Node.js backend is an important task. It can not only ensure the stability and reliability of the application, but also improve development efficiency.

In this article, we mainly introduce the deployment and testing methods of Node.js development environment and production environment. Of course, in practical applications, there are many specific details that need to be paid attention to, and we need to make selections and adjustments according to specific application scenarios.

We hope that through the introduction of this article, we can help developers better deploy and test the Node.js backend.

The above is the detailed content of nodejs backend deployment test interface. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template