- Web Development with MongoDB and Node(Third Edition)
- Bruno Joseph D'mello Mithun Satheesh Jason Krol
- 156字
- 2021-07-08 10:32:37
A simple server with Node.js
To see an example of how lightweight Node.js can be, let's take a look at some sample code that starts up an HTTP server and sends Hello World to a browser:
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080');
A few basic lines of code are all it takes to write a complete Node.js application. Running it with a simple Node.js app.js command will launch an HTTP server that is listening on port 8080. Point any browser to http://localhost:8080, and you will see the simple output Hello World on your screen! While this sample app doesn't actually do anything useful, it should give you a glimpse of the kind of power you will have while writing web applications using Node.js. If you don't have the initial Node.js development environment set up, we will discuss it in the next chapter.
推薦閱讀
- Java程序設計與開發
- 觸·心:DT時代的大數據精準營銷
- The Android Game Developer's Handbook
- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- Visual C++數字圖像模式識別技術詳解
- 實用防銹油配方與制備200例
- Programming ArcGIS 10.1 with Python Cookbook
- 編程可以很簡單
- 自學Python:編程基礎、科學計算及數據分析(第2版)
- Visual Basic程序設計全程指南
- Visual Basic程序設計實驗指導及考試指南
- Python機器學習開發實戰
- JavaScript Concurrency
- JavaScript Unit Testing
- H5匠人手冊:霸屏H5實戰解密