Loading basic HTML in Node.js

后端 未结 20 1337
暗喜
暗喜 2020-11-28 17:34

I\'m trying to find out how to load and render a basic HTML file so I don\'t have to write code like:

response.write(\'...

blahblahblah

...\
20条回答
  •  攒了一身酷
    2020-11-28 18:12

    This is an update to Muhammed Neswine's answer

    In Express 4.x, sendfile has been deprecated and sendFile function has to be used. The difference is sendfile takes relative path and sendFile takes absolute path. So, __dirname is used to avoid hardcoding the path.

    var express = require('express');
    var app = express();
    var path = require("path");
    
    app.get('/', function (req, res) {
        res.sendFile(path.join(__dirname + '/folder_name/filename.html'));
    });
    

提交回复
热议问题