【问题标题】:Is there a way for me to supply the http server in NodeJS with an html file for hosting?有没有办法为 NodeJS 中的 http 服务器提供一个用于托管的 html 文件?
【发布时间】:2022-02-04 01:22:32
【问题描述】:

当前代码如下

const http = require('http');
const server = http.createServer(function(req, res) {
  res.setHeader("Content-type", "application/json");
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.writeHead(200);
  res.end("Ping server page");
});

server.listen(8080, function() {
  console.eblue("\nListening on port 8080");
  console.eblue("WEB SERVER STARTED\n");
});

我只是想知道他们是否可以制作一个功能齐全的网站并使用 node.js 托管它

【问题讨论】:

标签: html node.js


【解决方案1】:

我建议对 Web 服务器使用 Express(一个 node.js 包),然后您可以通过 express.static 提供静态文件(包括 HTML)。

【讨论】:

    【解决方案2】:

    是的(如果我理解正确的话),您只需将 html 作为静态文件提供,如下所示:

    https://expressjs.com/en/starter/static-files.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2019-05-02
      • 2020-05-03
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多