- 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.
推薦閱讀
- 大學(xué)計(jì)算機(jī)基礎(chǔ)(第二版)
- TestNG Beginner's Guide
- Java編程指南:基礎(chǔ)知識(shí)、類(lèi)庫(kù)應(yīng)用及案例設(shè)計(jì)
- Python網(wǎng)絡(luò)爬蟲(chóng)從入門(mén)到實(shí)踐(第2版)
- JavaScript從入門(mén)到精通(第3版)
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- Mastering Python Networking
- 網(wǎng)站構(gòu)建技術(shù)
- Hands-On Functional Programming with TypeScript
- Swift語(yǔ)言實(shí)戰(zhàn)精講
- Django實(shí)戰(zhàn):Python Web典型模塊與項(xiàng)目開(kāi)發(fā)
- 計(jì)算機(jī)應(yīng)用技能實(shí)訓(xùn)教程
- Python程序設(shè)計(jì)教程
- Python硬件編程實(shí)戰(zhàn)
- Java程序設(shè)計(jì)入門(mén)(第2版)