Using node.js as a simple web server

后端 未结 30 2531
感情败类
感情败类 2020-11-22 02:54

I want to run a very simple HTTP server. Every GET request to example.com should get index.html served to it but as a regular HTML page (i.e., same

30条回答
  •  野的像风
    2020-11-22 03:17

    var http = require('http');
    var fs = require('fs');
    var index = fs.readFileSync('index.html');
    
    http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        // change the to 'text/plain' to 'text/html' it will work as your index page
        res.end(index);
    }).listen(9615);
    

    I think you where searching for this. In your index.html, simply fill it with normal html code - whatever you want to render on it, like:

    
        

    Hello world

提交回复
热议问题