我使用 node.js 作為服務器端來存盤 API 回應,該應用程式運行沒有任何問題,但最近我一直試圖在 Vercel 上托管它,所以我遇到了很多問題,專案卡在了大樓里程序...
建筑輸出:
建筑輸出
我的 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();
/* Middleware*/
//Here we are configuring express to use body-parser as middle-ware.
const bodyParser = require('body-parser');
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('website'));
// Setup Server
const port = 8000;
const Server = app.listen(port , run);
function run() {
console.log(`hello there :D`);
console.log(`here is ${port} ready to go`);
}
//GET method
app.get('/all', function(req,res){
res.send(projectData)
console.log(projectData);
})
//POST method
app.post("/addUserComment", function(req,res){
projectData = {
temp : req.body.temp,
date : req.body.date,
feeling : req.body.feeling,
}
console.log(projectData);
res.send(projectData);
})
我的作業目錄和構建設定:
作業目錄
構建設定
注意:server.js 是我的服務器端檔案,我的website檔案夾包括我的 app.js 檔案和我的 HTML、CSS 檔案,我也嘗試添加Vercel.json檔案但我不明白如何使用它,所以如果你要在您的答案中添加此檔案,請解釋如何以及為什么
uj5u.com熱心網友回復:
我認為您需要洗掉 build 命令,因為它現在正在嘗試運行 server.js 檔案而不是進行構建。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/486072.html
標籤:javascript 节点.js 表示 托管 韦尔塞尔
