Node.js: Difference between revisions

From 太極
Jump to navigation Jump to search
(Created page with "= node.js prompt = == How to exit == <pre> .exit </pre> = http server = <pre> # Terminal 1 node web-server.js # Terminal 2 curl http://localhost:8000 OR use "-i" to include HTT…")
 
Line 6: Line 6:


= http server =
= http server =
First create a js file
<pre>
var http = require('http');
var s = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello \n');
  setTimeout(function() {
    res.end("world\n");
  }, 2000);
});
s.listen(8000);
</pre>
Then using '''node''' program to run the js script. We can test the program by using a web browser or '''curl''' program.
<pre>
<pre>
# Terminal 1
# Terminal 1

Revision as of 14:19, 11 October 2013

node.js prompt

How to exit

.exit

http server

First create a js file

var http = require('http');

var s = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello \n');
  setTimeout(function() {
    res.end("world\n");
  }, 2000);
});

s.listen(8000);

Then using node program to run the js script. We can test the program by using a web browser or curl program.

# Terminal 1
node web-server.js

# Terminal 2
curl http://localhost:8000
OR use "-i" to include HTTP-header in the output
curl -i http://localhost:8000