Because different versions of Express.js's APIs differ, the code for this article is for Express 3.2.5
----Package.json
----Index.js
1. Create a file under the project directory, Package.json, which is the basis for the packages required for NPM installation projects.
Package.json
{ "name": "Hello-world", "description": "Hello World Test App", "version": "0.0.1", "private": true, "dependencies": { "Express": "3.2.5" }}
2. Open the project in the Command Line window so in the directory through NPM installation Project dependency package, after installation appears folder Node_modules, which contains the Express package:
NPM Install
3. Build file Index.js.Express provides "Routing" this thing, actually is, App.get (), App.post (), App.all (), this three function, the first parameter is the path, the first parameter is a callback function.
Index.js
var express = require ("Express"), var http = require ("http"), var app = Express (), App.all ("*", function (request, response, n EXT) { Response.writehead (404, {"Content-type": "Text/plain"}); Next ();}); App.get ("/", function (request, response) { Response.End ("Welcome to the homepage!");}); App.get ("/about", function (request, response) { Response.End ("Welcome to the About page!"); App.get ("*", function (request, response) { response.end ("404!");}); Http.createserver (APP). Listen (3000);
4. Browser-side Access URL:
http://127.0.0.1:3000
Browser-side Access URL:
Http://127.0.0.1:3000/about
Browser-side Access URL:
http://127.0.0.1:3000/*
Express.js----Routing