使用许多不同的html页面与快递

Using many different html page with express

本文关键字:快递 html 许多不      更新时间:2023-09-26

我正在学习节点js/express,并有一个快速的问题。在 express 中为您的 get 方法使用多个不同的 html 文件是否不好。例如,对于每个 get 方法,我打开一个不同的 html 文件。

app.get('/', function(req, res){
  var html = fs.readFileSync('index2.html');
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(html);
});

app.get('/continuous', function(req, res){
  var html = fs.readFileSync('index6.html');
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(html);
});
app.get('/output', function(req, res){
  var html = fs.readFileSync('index4.html');
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(html);
});

虽然并不总是有用的,但蛞蝓非常棒。

app.get('/:page', function(req, res) {
    res.sendFile(__dirname + '/' + req.params + ".html");
});

localhost/some-page的请求将返回来自同一目录的文件some-page.html

请注意不要在与敏感数据相同的目录中执行此操作。

您可以使用

res.sendFile使该过程更容易:

app.get('/game', function (req, res) {
    res.sendFile('/game.html', {
        root: path.join(__dirname, '/public')
    });
 });