Express.js----Routing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.