- Web Development with MongoDB and NodeJS(Second Edition)
- Mithun Satheesh Bruno Joseph D'mello Jason Krol
- 403字
- 2021-07-09 21:12:59
A simple server with Node.js
To see an example of just how lightweight Node 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 application. Running it with a simple Node 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.
When to use Node.js?
You should have heard of this proverb:
"If all you have is a hammer, everything looks like a nail!"
This makes a lot of sense in this context. Node.js is not a technology to depend on all the application problems that you intend to solve and if not chosen wisely, the decision will backfire. Node.js is well suited for applications that are expected to handle a huge amount of concurrent connections. Also, it should be noted that it is most suited for applications where each incoming request requires very few CPU cycles. This means that if you intend to do computation-intensive tasks on requests, it will end up blocking the event loop—thereby impacting other requests concurrently processed by the web server. Node.js is well suited for real-time web applications, such as chat rooms, collaboration tools, online games, and so on. So when deciding whether to use or not use Node.js, we should analyze the application context seriously and figure out whether Node.js really suits the context of the application.
Note
It is quite hard to debate over the use cases of Node.js in a detailed manner. However, the following stackoverflow thread does this so effectively and we strongly recommend you to go though the answers on this post if you are more interested in the use cases of Node.js: http://stackoverflow.com/questions/5062614/how-to-decide-when-to-use-node-js
As we have briefly gone through the features and concept of Node.js, now let's look into the NoSQL and MongoDB side.
- UML和模式應用(原書第3版)
- The React Workshop
- Java深入解析:透析Java本質的36個話題
- Elasticsearch for Hadoop
- 小學生C++創意編程(視頻教學版)
- Learning R for Geospatial Analysis
- Cocos2d-x by Example:Beginner's Guide(Second Edition)
- Robot Framework Test Automation
- MATLAB 2020 GUI程序設計從入門到精通
- Mastering ArcGIS Server Development with JavaScript
- Android開發進階實戰:拓展與提升
- 生成藝術:Processing視覺創意入門
- Python程序設計
- Computer Vision with Python 3
- Instant SQL Server Analysis Services 2012 Cube Security