Series: Introduction to the MEAN Stack
- Part 1: Definition of the MEAN stack
- Part 2: Setup of the MEAN stack
- Part 3: Node.js
- Part 4: npm
- Part 5: Connect
- Part 6: Express
- Part 7: MongoDB
- Part 8: Mongoose
- Part 9: REST
- Part 10: Baucis
- Part 11: Bower
- Part 12: AngularJS
- Part 13: Restangular
In this article, I tell you what Node.js is exactly and what particular features it has. We also program a working server in five code lines!
Node.js (short node) is a server-side runtime environment for JavaScript based on Google’s V8 engine . Node uses an event-based and non-blocking input-output model and is thus suitable for fast and scalable network applications. But what does this mean in practice?
The process of calling a web page with the LAMP stack mentioned in the first article is approximately as follows: The browser starts a request with a URL that arrives at the Apache server. Depending on the URL, Apache calls various PHP scripts, which build a response – the actual website. The PHP scripts can retrieve data from the MySQL database while filling the response with these data. Once all PHP scripts have been processed, the website has been completely output. Only now can new requests for responses of PHP be converted.
This is different for Node. Node takes on the tasks of Apache and PHP alike. It accepts a request and calls corresponding JavaScript scripts to respond with a response. Data queries from Node can be sent to a MongoDB database (or any other database). Already now node can start work off new requests because the query the database takes some time to complete. This time would be unused for the LAMP stack. When the database query is terminated, Node reverts the original request to a response as soon as the corresponding process time is free. This jump back to a previous request is made via callbacks solved, functions that are executed when a particular event (such as the end of a database query) arrives.
However, this does not exactly correspond to reality. Through other threads or processes, the LAMP stack can process further requests for responses despite a running database query. However, their number is limited by the hardware and thus are scaled.
Node was developed by Ryan Dahl, who has given the project responsibility to Isaac Schlueter.
In essence, Node is a C ++ project, but its APIs are controlled by JavaScript.
This series of articles is primarily intended to explain the MEAN stack in a practical way. For this reason, in the following example, a complete HTTP server with a node is already being programmed with this short definition. Just a few lines of code are sufficient:
1
2 3 4 5 |
var http = require ( ‘http’ ) ;
http. createServer ( function ( req , res ) { res. writeHead ( 200 , { ‘ ContentType ‘ : ‘text / plain’ } ) ; res .end ( ‘HelloWorld \ n ‘ ) ; } ) . listen ( 1337 , ‘127.0.0.1’ ) ; |
Saves this code to a file called server.js
and then starts the following command with the node server ( must be replaced by you with the real path to ):/pfad/zu
server.js
1
|
$ node / path / to / server.js
|
If you now enter the address line in any browser http://127.0.0.1:1337/ , you will get a “Hello World”.
With a ^ C within the terminal you can terminate the Node server.
What happened? require
The module is http
called with the function in the first code line . The http
module has the function createServer
, which literally a server can create. The test in the browser has proved it! The function contains a callback with the two parameters req
and res
, which are for request and response. The callback is called at each request at http://127.0.0.1:1337/ . The req
object contains, for example, information about the URL, which is not yet used in this example. The res
text “Hello World” is output from the object.
By the way: Node must be restarted for each code change, so that the change takes effect! This is not explicitly mentioned in the following examples and should therefore be kept in mind.
Any node code is organized in modules based on the CommonJS standard . http
is a so-called core module , which is a module, which is delivered with Node itself. All core modules and their APIs can be found at http://nodejs.org/api/ . A component of CommonJS is the function require
that loads the modules. Because each node code is in modules, they can always be required . Without knowing it, we have written with the file server.js
our first node module, which can be required by other modules. We can also test this with another module, which we store in a file :require-server.js
1
|
var http = require ( ‘./server’ ) ;
|
The file contains only one code line and must be in the same folder as . Use the following command to run the file in Node:require-server.js
server.js
1
|
$ node / path / to / require – server.js
|
If you now call again http://127.0.0.1:1337/ in the browser, then you get as before a “Hello World”. The inside of shows that a module should be loaded in the same folder with the filename . The file transmission can be omitted since it is automatically accepted by Node../
require
server
.js
In order for you to create a useful application from this simple and still useless server, we need a few frameworks that help us to develop. How you can easily manage your frameworks (or Node Module), I’ll show you in the next article.
0 Comments