Difference between revisions of "Nodejs: web server sederhana"

From OnnoWiki
Jump to navigation Jump to search
 
Line 45: Line 45:
  
 
* http://stackoverflow.com/questions/6084360/node-js-as-a-simple-web-server
 
* http://stackoverflow.com/questions/6084360/node-js-as-a-simple-web-server
 +
* http://chat.nodejs.org/

Latest revision as of 05:21, 6 May 2014

Sumber: http://stackoverflow.com/questions/6084360/node-js-as-a-simple-web-server

Web Server Amat Sangat Sederhana

Simplest nodejs server is just ....

$ npm install http-server -g

Now you can run a server via

$ cd MyApp
$ http-server

OR

$ http-server -o --cors - which opens your web browser and enables CORS requests.

For more options, check out https://github.com/nodeapps/http-server OR run:

$ http-server --help


Web Server

Sumber: http://chat.nodejs.org/


An example: Webserver

This simple web server written in Node responds with "Hello World" for every request.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

To run the server, put the code into a file example.js and execute it with the node program from the command line:

% node example.js
Server running at http://127.0.0.1:1337/

Referensi