Nodejs build servers without express.

  node.js, question

How to build nodejs server without express framework, with a page, ejs template engine and a js file, which can be used for file operation

Don’t have toexpressDirect build, wait for you to solve all the pit, estimates that either become a great god, or become a big nerve!

However, since I have this hobby, I’ll give you a simple brainless version:

'use strict';
 var http = require('http');
 var ejs = require('ejs');
 
 var server = http.createServer();
 server.on('request', function(request, response) {
 var url = request.url;
 //Different methods can be used here according to different url.
 
 Response.end (ejs.render ('< html > < body > < h1 > hello, < percentage = name percentage >!  </h1></body></html>', {
 name: 'World'
 }));
 });
 
 server.listen(8080);

The prerequisite is to install it first.ejsOh,npm install ---save ejs

Then when you visit the browserhttp://localhost:8080, the following interface will be presented:

图片描述