问题描述
我正在按照此教程一切顺利,但是我需要改变通过配置文件为应用程序提供服务的基本根目录.
I'm building a Q&A app following this tutorial and everything goes well, but I need to change the chance to change the base root where the app is being served via config files.
现在该应用程序在localhost:8080中提供,而我需要在localhost:8080/qae中提供(例如).
Now the app is served in localhost:8080 and I need to be served over localhost:8080/qae (for example).
我认为答案就在这段代码附近:
I think the answer is near this piece of code:
// Setup server
var app = express();
var server = http.createServer(app);
var socketio = require('socket.io')(server, {
serveClient: config.env !== 'production',
path: '/socket.io-client'
});
require('./config/socketio')(socketio);
require('./config/express')(app);
require('./routes')(app);
// Start server
function startServer() {
app.angularFullstack = server.listen(config.port, config.ip, function() {
console.log('Express server listening on %d, in %s mode '+config.ip, config.port, app.get('env'));
});
}
setImmediate(startServer);
(来自/server/app.js)
(from /server/app.js)
但是我不知道.可以用简单的方式做到这一点吗?
But I can't figure it. Is it possible doing this in a simple way?
/////////EDIT////////
////////EDIT////////
我尝试了所有建议的解决方案,但是我做错了事,但遇到了错误.如果有帮助,这是我的routes.js:
I tried all the proposed solutions, but I'm doing something wrong and got errors. This is my routes.js in case it helps:
/**
* Main application routes
*/
'use strict';
import errors from './components/errors';
import path from 'path';
export default function(app) {
// Insert routes below
app.use('/api/cpd', require('./api/cpd'));
app.use('/api/categories', require('./api/category'));
app.use('/api/terms', require('./api/term'));
app.use('/api/qae', require('./api/qae'));
app.use('/api/stats', require('./api/stat'));
app.use('/api/tags', require('./api/tag'));
app.use('/api/questions', require('./api/question'));
app.use('/api/things', require('./api/thing'));
app.use('/api/users', require('./api/user'));
app.use('/auth', require('./auth'));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// All undefined asset or api routes should return a 404
app.route('/:url(api|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
// All other routes should redirect to the index.html
app.route('/*')
.get((req, res) => {
res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
});
}
推荐答案
您应该更改此设置的根源:
You should change your rooting to this:
app.use('/qae',require('./routes'))
,在routes/index.js
中,您可以拥有路线的所有声明.
and in routes/index.js
you can have all declarations of your routes.
在routes.js
export default function(app) {
// Insert routes below
app.use('/qae', require('./api'));
app.use('/auth', require('./auth'));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// All undefined asset or api routes should return a 404
app.route('/:url(api|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
// All other routes should redirect to the index.html
app.route('/*')
.get((req, res) => {
res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
});
}
在api
const express = require('express')
const router = express.Router()
router.use('/api/cpd', require('./cpd'));
router.use('/api/categories', require('./category'));
router.use('/api/terms', require('./term'));
router.use('/api/qae', require('./qae'));
router.use('/api/stats', require('./stat'));
router.use('/api/tags', require('./tag'));
router.use('/api/questions', require('./question'));
router.use('/api/things', require('./thing'));
router.use('/api/users', require('./user'));
module.exports = router
这样,您的所有api路由都将类似于/qae/api/*
.如果在该前缀之后还需要auth
,则需要使用相同的方法.最好的解决方案是让我app.use('/',...)
包括子文件夹中的路由器.
That way all your api routes will look like /qae/api/*
. If you need auth
also after this prefix you need to do it same way.Best solution is to have i app.use('/',...)
including routers from subfolders.
这篇关于Express应用-更改基本网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!