nodejs modular programming

0 install nodejs, see webpack installation

1. custom modules and call

1) Create a demo1_1.js

exports.add=function(a,b){
    return a+b;
}

2) Create a demo1_2.js in the same directory

var demo=require('./demo1_1');
console.log(demo.add(41,5));

3) In the command line node demo1_2, results showed that 46

2. Call nodejs module comes with the official document: http://nodejs.cn/api/ . example:

1) Create a demo2.js

var HTTP = the require ( 'HTTP' ); 
http.createServer ( function (Request, Response) {
 // HTTP headers are sent 
// HTTP status values: 200 is: the OK 
// Content Type: text / Plain 
response.writeHead (200 is, { 'the Type-the Content': 'text / Plain' });
 // send a response "the Hello World" 
Response.End ( 'the Hello World \ n-' ); 
}) the listen (. 8888);

2) execution node demo2

3) browser to access localhost: 8888, you can see "Hello World"

Guess you like

Origin www.cnblogs.com/naixin007/p/11257766.html