search

Home  >  Q&A  >  body text

node.js - nodejs搭建服务器,不用express怎么写?

如何不用express框架搭建nodejs服务器,有一个页面,使用ejs模板引擎,还有一个js文件,里面可以进行文件操作

伊谢尔伦伊谢尔伦2863 days ago607

reply all(3)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:00:37

    No need to express just take it. Once you solve all the pitfalls, you will probably become either a great god or a great neurotic!

    But since you have this hobby, I will give you a simple and 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;
        //这里可以根据url的不同使用不同的方法来处理
    
        response.end(ejs.render('<html><body><h1>Hello, <%= name %>!</h1></body></html>', {
            name: 'World'
        }));
    });
    
    server.listen(8080);

    The prerequisite is to install ejs first, npm install ---save ejs

    Then when you access the browser http://localhost:8080, the following interface will appear:

    reply
    0
  • 黄舟

    黄舟2017-04-17 14:00:37

    Node provides relatively low-level interfaces. Even basic functions such as cookie, session, get, post, etc. need to be implemented by yourself, so if you don’t want to reinvent the wheel yourself, you should use the ready-made express framework.

    reply
    0
  • PHPz

    PHPz2017-04-17 14:00:37

    To support the original poster’s reinvention, you can take a look at the http module

    reply
    0
  • Cancelreply