[英]Why the subdomains are not working with express.js?
so I am experimenting with it locally, this is in my hosts file 所以我正在本地进行实验,这在我的主机文件中
127.0.0.1 example.dev
127.0.0.1 www.example.dev
127.0.0.1 api.example.dev
and this is my code: 这是我的代码:
var subdomain = require('express-subdomain');
var express = require('express');
var app = express();
var router = express.Router();
// example.com
app.get('/', function(req, res) {
res.send('Homepage');
});
//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);
it's basically the example from the package website api.example.dev/users works well, but when I go to to api.example.dev the content is the same as on example.dev (like it is overwritten) any ideas what I am doing wrong? 这基本上是来自软件包网站api.example.dev/users的示例,效果很好,但是当我转到api.example.dev时,内容与example.dev相同(就像被覆盖了一样),我的想法是什么做错了吗? thanks 谢谢
This is a order of requests processing problem. 这是请求处理问题的顺序。 Move the declaration of the request handler for the main domain after the subdomain: 将主域的请求处理程序的声明移到子域之后:
var subdomain = require('express-subdomain');
var express = require('express');
var app = express();
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));
// example.com
app.get('/', function(req, res) {
res.send('Homepage');
});
app.listen(3000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.