我对npm docs上的示例“划分并征服”部分进行了一些修改:https://www.npmjs.com/package/express-subdomain
const subdomain = require('express-subdomain');
const express = require('express');
const app = express();
var router = express.Router(); //main api router
var v1Routes = express.Router();
v1Routes.get('/test', function(req, res) {
res.send('API - version 1');
});
router.use(subdomain('api', v1Routes));
//basic routing..
router.get('/', function(req, res) {
res.send('Welcome to the API!');
});
//attach the api
app.use(subdomain('api', router));
app.listen(8000);
console.log("listening on 8000")
基本上,我摆脱了v2路由,并将v1路由上的子域更改为api
。我也摆脱了中间件,将v1路由更改为/test
只是为了将其与基本路由区分开。
我的/ etc / hosts文件也看起来像这样:
127.0.0.1 myapp.dev
127.0.0.1 api.myapp.dev
255.255.255.255 broadcasthost
::1 localhost
我唯一要做的就是将localhost
更改为myapp.dev
,并添加了api.myapp.dev
,我在这里找到:https://github.com/bmullan91/express-subdomain#developing-locally
有什么想法吗?