簡體   English   中英

如何使用 HTML 文件連接到使用 Express 的 Node.js 服務器?

[英]How to connect to a Node.js server that's using Express using an HTML file?

您在 LocalHost 或服務提供商(Heroku、AWS)上運行node.js express 服務器。 如何將其連接到我的網站?

EG -- You go to.. www.yourdomain.com -> Connects to HEROKU server -> HEROKU server sends html file

應用程序.js

const http      = require('http'),
      path      = require('path'),
      express   = require('express'),
      app       = express(),
      serv      = require('http').Server(app);

var htmlPath = path.join(__dirname, 'client');

app.use(express.static(htmlPath));

app.get('/', (req, res) => {
    res.sendFile(htmlPath + '/index.html');
});

var server = serv.listen(2000, () => {
    var host = 'localhost';
    var port = server.address().port;
    console.log(`listening on http://${host}:${port}/`);
});

In this case, now instead of going to localhost:2000 directly to receive the index.html file, you go to yourdomain.com and the server sends the index.html file

您需要將 DNS 系統設置為重定向到托管 Express 應用程序的機器的公共 IP。 快速應用程序還需要偵聽發送到您指定的路徑的 GET 請求。

您還需要在該計算機上配置防火牆以允許打開端口 80 和 443。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM