我正在使用 https://www.npmjs.com/package/express-subdomain 中的示例设置包裹。但是,当我在浏览器中尝试 api.localhost:3000 时,它会抛出错误:“Cannot GET/”
如何让 api.localhost:3000 运行?我会考虑其他解决方案,而不仅仅是使用 express-subdomain
的解决方案,只要您不必在设置中输入 FQDN。
app.js:
var subdomain = require('express-subdomain');
var express = require('express');
var app = express();
// *** Code examples below go here! ***
var router = express.Router();
//api specific routes
router.get('/', function(req, res) {
res.send('Welcome to our API!');
});
router.get('/users', function(req, res) {
res.json([
{ name: "Brian" }
]);
});
app.use(subdomain('api', router));
app.listen(3000);
package.json:
{
"name": "auth_manager",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"express-subdomain": "^1.0.5"
}
}
最佳答案
我认为该 API 处于事件状态,位于 http://api.example.com/而不是在 api.localhost:3000 中。但是,如果您想在本地运行它们,我建议您遵循 https://www.npmjs.com/package/express-subdomain#developing-locally
关于node.js - Express-子域看不到路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54876681/