首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Node应用程序cPanel从本地主机部署到服务器

如何将Node应用程序cPanel从本地主机部署到服务器
EN

Stack Overflow用户
提问于 2022-01-14 12:06:50
回答 2查看 169关注 0票数 1

我已经创建了一个简单的Express JS应用程序。而且它在当地运行得很好。当我访问localhost:8000时,我会看到静态文件(index.html、style.css和frontend.js)。

我曾尝试使用cPanel在服务器上部署该应用程序。我已经成功地使用package.json安装了Node和依赖项。但是当我访问域时,我只看到一条消息(Node应用程序正在工作,Node版本是10.24.1)。

如何使我的应用程序指向并显示静态文件夹(index.html)并运行该应用程序?

我的应用架构:

  • server.js
  • package.json
  • public/index.html
  • public/style.css
  • public/frontend.js

下面是我的server.js启动文件:

代码语言:javascript
复制
// 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);
}
EN

回答 2

Stack Overflow用户

发布于 2022-01-14 12:16:25

这里的问题是,您没有指出发送HTML文件的路径。否则,客户端必须将其指向文件的正确路径,如localhost:3000/index.html。您需要使用app.get从服务器发送它。

代码语言:javascript
复制
app.get("/", (req, res) => {
  res.sendFile(__dirname + "path to the file");
});
票数 0
EN

Stack Overflow用户

发布于 2022-01-14 14:43:04

问题是,我在域的一个子文件夹中创建了该应用程序。但是当我创建子域并在其中重新安装应用程序时,应用程序成功地指向静态文件夹。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70710327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档