我当前的代码是:
var express = require('express');
const app = express();
var port = process.env.PORT || 3000;
app.get('/', function(req, res, next) {
res.redirect('/api')
});
app.get('/api', function (req, res) {
res.json({
text: 'my api!'
})
});
app.listen(port, function () {
console.log('App listening on ' + port + '!');
});
上面的代码在localhost上正常工作,但是当部署到我正在使用的服务器上时,会出现以下错误:
无法获取/API
未能加载资源:服务器响应状态为404(未找到)
你能试试下面的吗??
app.listen(port, <your public ip>, () => {
console.log('App listening on ' + port + '!');
});
为了使它在任何服务器上工作,您必须定义您的端口。
var express = require('express');
const app = express();
var port = process.env.PORT || 3000;
app.get('/', function(req, res, next) {
res.redirect('/api')
});
app.get('/api', function (req, res) {
res.json({
text: 'my api!'
})
});
//You can bind the IP address using the following code
app.listen(port, '<ip>', function () {
console.log('App listening on ' + port + '!');
});
正如我在您的代码中看到的,您可以访问3000
上的服务器
或者如果您有.env
必须显式定义端口号。
PORT=80 //this may change depending on your choice
然后访问您的服务器http://:port