我已经创建了一个简单的Express JS应用程序。而且它在当地运行得很好。当我访问localhost:8000时,我会看到静态文件(index.html、style.css和frontend.js)。
我曾尝试使用cPanel在服务器上部署该应用程序。我已经成功地使用package.json安装了Node和依赖项。但是当我访问域时,我只看到一条消息(Node应用程序正在工作,Node版本是10.24.1)。
如何使我的应用程序指向并显示静态文件夹(index.html)并运行该应用程序?
我的应用架构:
下面是我的server.js启动文件:
// Setup empty JS object to act as endpoint for all routes
projectData = {};
// Require Express to run server and routes
const express = require('express');
// Start up an instance of app
const app = express();
/* Dependencies */
const bodyParser = require('body-parser');
/* Middleware*/
//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Cors for cross origin allowance
const cors = require('cors');
app.use(cors());
// Initialize the main project folder
app.use(express.static('public'));
// Setup Server
const port = 8000;
const server = app.listen(port, function(){
console.log(`server running on localhost: ${port}`);
});
//POST Route to store data in the app endpoint, projectData object
app.post('/addData', addData);
function addData (req, res){
let data = req.body;
projectData = data;
console.log(projectData);
}
app.get('/getData', getData);
function getData(req, res) {
res.send(projectData);
}发布于 2022-01-14 12:16:25
这里的问题是,您没有指出发送HTML文件的路径。否则,客户端必须将其指向文件的正确路径,如localhost:3000/index.html。您需要使用app.get从服务器发送它。
app.get("/", (req, res) => {
res.sendFile(__dirname + "path to the file");
});发布于 2022-01-14 14:43:04
问题是,我在域的一个子文件夹中创建了该应用程序。但是当我创建子域并在其中重新安装应用程序时,应用程序成功地指向静态文件夹。
https://stackoverflow.com/questions/70710327
复制相似问题