nodejs - hello world

Node

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var http = require("http");

http.createServer(function (req, res) {

// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
res.writeHead(200, {'Content-Type': 'text/plain'});

// Send the res body as "Hello World"
res.end('Hello World\n');
}).listen(3000);

// Console will print the message
console.log('Server running at http://127.0.0.1:3000/');

Node + Express

index.js file

1
2
3
4
5
6
7
8
9
10
let express = require("express");
let app = express();

app.get("/", function (req, res) {
res.send("hello world");
});

app.listen(3000, function () {
console.log("Server Started");
});

ES6

1
2
3
4
5
6
const express = require('express');
const app = express();

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(3000, () => console.log('Example app listening on port 3000!'));