Node.js Notes for Professionals

This Node.js Notes for Professionals book is compiled from Stack Overflow Documentation, the content is written by the beautiful people at Stack Overflow. Text content is released under Creative Commons BY-SA, see credits at the end of this book whom contributed to the various chapters. Images may be copyright of their respective owners unless otherwise specified
This is an unofficial free book created for educational purposes and is not affiliated with official Node.js group(s) or company(s) nor Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners
The information presented in this book is not guaranteed to be correct nor accurate, use at your own risk
Please send feedback and corrections to [email protected]

First, install Node.js for your platform.
In this example we'll create an HTTP server listening on port 1337, which sends Hello,
that, instead of using port 1337, you can use any port number of your choice which is currently not in use by any other service.
The http module is a Node.js core module (a module included in Node.js's source, that does not require installing additional resources). The http module provides the functionality to create an HTTP server using the http.createServer() method. To create the application, create a file containing the following JavaScript code.
World! to the browser. Note
const http = require('http'); // Loads the http module http.createServer((request, response) => {
// 1. Tell the browser everything is OK (Status code 200), and the data is in plain text
response.writeHead(200, {
'Content-Type': 'text/plain'
// 2. Write the announced text to the body of the page
response.write('Hello, World!\n');
// 3. Tell the server that all of the response headers and body have been sent
}).listen(1337); // 4. Tells the server what port to be on
Save the file with any file name. In this case, if we name it hello.js we can run the application by going to the directory the file is in and using the following command:
node hello.js

  • Tác giả:
  • Số trang: 334
  • Lượt xem: 574
  • Lượt tải: 4
  • Đã đọc: 34
  • Kích thước: 2.79 Mb