Get started with ExpressJS and get started with expressjs
1. Create a project directory.
Copy codeThe Code is as follows:
> Md hello-world
2. Go to this directory and define the project configuration file package. json.
For accurate definition, run the following command:
Copy codeThe Code is as follows:
D: \ tmp \ node \ hello-world> npm info express version
Npm http GET https://registry.npmjs.org/express
Npm http 200 https://registry.npmjs.org/express
3.2.1
Now that the latest version of the ExpressJS framework is 3.2.1, the configuration file is:
Copy codeThe Code is as follows:
{
"Name": "hello-world ",
"Description": "hello world test app ",
"Version": "0.0.1 ",
"Private": true,
"Dependencies ":{
"Express": "3.2.1"
}
}
3. Use npm to install the project dependent package.
Copy codeThe Code is as follows:
> Npm install
Once the dependency package is installed in npm, The node_modules subdirectory will appear in the project root directory. The express packages required for project configuration are stored here. If the verification is performed, run the following command:
Copy codeThe Code is as follows:
> Npm ls
Ps d: \ tmp \ node \ hello-world> npm ls
Npm WARN package. json hello-world@0.0.1 No README. md file found!
Hello-world@0.0.1 D: \ tmp \ node \ hello-world
└ ── ┬ Express@3.2.1
── Buffer-crc32@0.2.1
── Commander@0.6.1
├ ── ┬ Connect@2.7.7
│ ── Bytes@0.2.0.
│ ── Formidable@1.0.13.
│ ── Pause@0.0.1.
── Cookie@0.0.5
── Cookie-signature@1.0.1
── Debug@0.7.2
── Fresh@0.1.0
── Methods@0.0.1
── Mkdirp@0.3.4
── Qs@0.6.1
── Range-parser@0.0.4
└ ── ┬ Send@0.1.0
── Mime@1.2.6
This command shows the express package and its dependency.
4. Create an application
Create the application itself. Create a file named app. js or server. js. Reference express and use express () to create a new application:
Copy codeThe Code is as follows:
// App. js
Var express = require ('express ');
Var app = express ();
Next, we can use app. verb () to define the route.
For example, to use the "GET/" response "Hello World" string, because res and req are the precise objects provided by Node, you can call res. pipe () or req. on ('data', callback) or others.
Copy codeThe Code is as follows:
App. get ('/hello.txt', function (req, res ){
Var body = 'Hello world ';
Res. setHeader ('content-type', 'text/plain ');
Res. setHeader ('content-length', body. Length );
Res. end (body );
});
The ExpressJS framework provides higher-level methods, such as res. send (), which can save things such as adding Content-Length. As follows:
Copy codeThe Code is as follows:
App. get ('/hello.txt', function (req, res ){
Res. send ('Hello World ');
});
Now you can bind and listen to the port. Call the app. listen () method to receive the same parameters, for example:
5. Run the program
Run the following command to run the program:
Copy codeThe Code is as follows:
> Node app. js
Access address in a browser: http: // localhost: 3000/hello.txt
The output result is as follows:
Copy codeThe Code is as follows:
Hello World