preface

Now the company uses Node locally to separate the front and back ends, and the server uses the deployment mode of Nginx + Tomcat. In fact, the technology of front and back end separation can be applied to Node deployments.


Local preparation


You only need to prepare the following files locally:

2) Package. json: If you’re deploying with Node, you’ll definitely use some of Node’s modules and frameworks, such as Path and Express. This JSON file is used to record the modules and frameworks that Node depends on, as follows:

{
  "name": "app-server"."version": "1.0.0"."description": ""."main": "index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": ""."license": "ISC"."dependencies": {
    "express": "^ 4.16.3"."path": "^ 0.12.7"}}Copy the code

3) server.js: the main file used to build the Node server. The content is as follows:

Var express = require('express');
var app = express();
var path = require('path'); // Set the login page and the home page access path app.get('/index'.function (req, res) {
    res.sendFile(__dirname + '/' + 'view/html/index.html');
});
app.get('/login'.function (req, res) {
    res.sendFile(__dirname + '/' + 'view/html/login.html');
});
app.get('/'.function (req, res) {
    res.sendFile(__dirname + '/' + 'view/html/login.html'); }); // Manage static file app.use('/static', express.static(path.join(__dirname, 'static')));
app.use('/view', express.static(path.join(__dirname, 'view'))); Var server = app.listen(8080,'localhost'.functionAddress var port = server.address().port console.log() {// (3.2.3) var host = server.address().address var port = server.address().port console.log()'Application instance, access at http://%s:%s', host, port)
});
Copy the code

The above code does two main things: routing control and setting up a server.


Start the deployment


1) Copy files

  1. Log in to your server (my server is Windows)
  2. Copy the local file above to the serverC:\Homedirectory

2) Install Node and nginx

Nginx-1.15.3 Installation file: pan.baidu.com/s/17H-S-zdF… Extract code: P3KS download after I was put in the server C disk root directory.

Node V8.11.2 Installation file: pan.baidu.com/s/17H-S-zdF… Extraction code: P3KS is downloaded and installed on the server. The installation options are selected by default, and environment variables will be automatically configured.

3) Configure Node and nginx

  1. Start the Node service: open CMD and enterC:\Home\web\In the path, run the command in sequencenpm installInstall dependencies andnode server.jsStart the service. If the following information is displayed, the service is successfully started.

  1. Nginx start:
  • Modify the nginx configuration file (skip this step if your Nginx is from a shared web disk) to openC: \ nginx - 1.15.3 \ conf \ nginx. ConfFile, find the server node, modify the configuration:
  • Open another CMD and enterC: \ nginx - 1.15.3 \In the directory, runstart nginx.exeIf no error is reported, nginx is successfully started, as shown below.

4) Deployment complete, you can start using the Internet to access your deployed website.

Here is the login page