javascript.get函数在开发中有效,但在生产中无效



下面是客户端尝试从服务器获取数据的代码。在node.js上本地运行我的服务器,效果很好。但是,当我推送到服务器时,我会得到/getSettings 500 (Internal Server Error)。当另一个请求是'/'时,我的索引会被命中,所以我知道端点可以被命中。但为什么不让另一个得到呢?

Node.js作为运行时在我的服务器上运行。

SERVER代码(app.js(:

const express = require('express');
var path = require('path');
var Config = require('./config.js'), conf = new Config();
const app= express();
const port = 3000;
app.listen(port, function(error){
if(error) {
console.log('Server failed to listen: ', error)
} else{
console.log('Server is listening on port: ' + port)
}
});
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get("/getSettings", function(require, response){
response.json({ configuration: conf });
});
app.use(express.static(path.join(__dirname, 'public')));

客户代码:

<script>
window.onload = function() 
{   
$.get("/getSettings", function( data ) 
{
//do stuffs
});
};
</script>

我找到了一个修复程序。/getSettings需要一个名为getSettings的空文件夹。因此,如果其他人的root运行良好,但没有其他路由,这可能是您的问题。

最新更新