How to serve html files with node.js express

 

(UPDATE 2016/18/03) This post explains how to serve easily html files with node.js and express module.


 

To do before reading

Comprendre node.js;
Installer node.js.

 

Create project folder

Create a new folder for the project files.

 

node.js express, installation

To use express library, you must install it. You just have to execute next command in your project folder:

npm install express

 

How to create a web server with node.js express

To use node.js express module, it must be declare a variable named express.

var express = require('express');

And instanciate server :

var server= express();

Finally, listen with the server on specific port, like in example 8050 :

server.listen(8050);

Final javascript code :

var express = require('express');
var server= express();
server.listen(8050);

To run node.js server, just call the next command :

node exemple.js

All code updates needs to stop node.js (by CTRL + C), and restart it.

Until this time, there are no difficulty. However, if you test the server from your navigator with the url http://localhost, you will see the message cannot GET.

 

How to server html page with node.js express

In the folder project, create a new html page named simple.html :

<html>
 <body>
  SIMPLE VIEW
 </body>
</html>

Server object provides get method to treat get http request and needs some parameters :
– url in which get function is calling;
– a callback function with request object and response object in parameters.

Below, we define / url accessible through http://serverAddress:8050/

server.get('/', function(request, response) {
 ....
});

In your browser, when querying http://adresseServeur:8050/ url, callback function is called.

Other example :

server.get('/fairedesjeux', function(request, response) {
 ....
});

From your browser, when querying http://adresseServeur:8050/fairedesjeux, callback function is called.

That is the callback function that would be used to serve html page with the url.

Serve a html page consists of returning html page by sendfile method from response object.

server.get('/simple.html', function(request, response) {
 response.sendfile('./simple.html');
});

Url and sended file can be different :

server.get('/pagesimple', function(request, response) {
 response.sendfile('./simple.html');
});

In this case, to display simple.html page, you have to call http://adresseServeur:8050/pagesimple from your browser.

Code :

var express = require('express');
var server = express();
server.get('/simple.html', function(request, response) {
	response.sendfile('./simple.html');
});
server.listen(8050);

That’s not a good solution to make a web server, it’s not made for. It require to you to declare all url paths.
Apache http Web Server is made for it.

So, why would we use node.js ? Just for network game programming.

[important]Comments, improvement, idea : let me know. If this post has been helpful, make comments on your favorite social networks.[/important]

Source node.js express : node.js express api.

 

Posté dans node.jsTaggé node.js, node.js page html, nodejs, nodejs page html  |  Laisser un commentaire

Répondre