NodeJS

Express.js Helloworld example

posted by tungnt 01/08/2016 0 comments

First create a directory named myapp, change to it and run npm init. Then install express as a dependency, as per the installation guide.

In the myapp directory, create a file named app.js and add the following code:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
    res.send('Hello World!');
});

app.get('/home', function (req, res) {
    res.send('home');
});

app.get('/about', function (req, res) {
    res.send('about');
});

app.listen(3000, function () {
    console.log('Example app listening on port 3000!');
});

Run the app with the following command:

$ node app.js

Then, load http://localhost:3000/ in a browser to see the output.

Bình luận
1257 Total Views 4 Views Today

You may also like