Node.js Generate html

前端 未结 4 2001
醉话见心
醉话见心 2020-12-02 11:17

I have created a JavaScript program which generates a list of data. Example output below:

output one 
output two 
output three 
...

I would

4条回答
  •  遥遥无期
    2020-12-02 12:06

    If you want to create static files, you can use Node.js File System Library to do that. But if you are looking for a way to create dynamic files as a result of your database or similar queries then you will need a template engine like SWIG. Besides these options you can always create HTML files as you would normally do and serve them over Node.js. To do that, you can read data from HTML files with Node.js File System and write it into response. A simple example would be:

    var http = require('http');
    var fs   = require('fs');
    
    http.createServer(function (req, res) {
      fs.readFile(req.params.filepath, function (err, content) {
        if(!err) {
          res.end(content);
        } else {
          res.end('404');
        }
      }
    }).listen(3000);
    

    But I suggest you to look into some frameworks like Express for more useful solutions.

提交回复
热议问题