Install nodejs environment under win7

1. Download the installation file from the official website

  The Node.js installation package and source code download address is: https://nodejs.org/en/download/.

  I installed to the D drive, the problem is not big.

Found some test code

1  var http = require('http' );
 2  
3 http.createServer( function (request, response) {
 4  
5      // Send HTTP header 
6      // HTTP status value: 200 : OK 
7      // Content type: text/ plain 
8      response.writeHead(200, {'Content-Type': 'text/plain' });
 9  
10      // Send response data "Hello World" 
11      response.end('Hello World\n' );
 12 }) .listen(8888 );
 13  
14  // The terminal prints the following information 
15console.log('Server running at http://127.0.0.1:8888/');
server.js

Execute on the command line:

node server.js

Server running at http://127.0.0.1:8888/

Open the browser and visit http://127.0.0.1:8888/, you can see the webpage that says "Hello World".

2. Install the express module

  Execute on the command line: npm install -g express

After executing the above command, you can see that express has been installed. However, when executing the code that references the express module, it prompts "Cannot findmodule 'express' ", re-execute: npm install express --save, re-execute the program OK.

  The parameters of npm here are all "--", I don't know why it is so unique.

1  // Introduce express module 
2  var express = require('express' );
 3  
4  // Create express instance 
5  var app = express();
 6  
7  // Response HTTP GET method 
8 app.get('/', function (req, res) {
 9    res.send('Hello World!' );
 10  });
 11  
12  // Listen to port 
13 on 8000 app.listen(8000, function () {
 14    console.log('Hello World is listening at port 8000' );
 15 });
hello_express.js

Let's go here for now.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856655&siteId=291194637