【发布时间】:2018-09-21 14:30:40
【问题描述】:
我今天遇到了一个与路由有关的问题。我有两个主要代码:一个是前端,另一个是后端。
前端是使用 Vue.js 编写的,所以它是一个 SPA。这个 web 应用有点复杂,涉及大量路由和后端 AJAX API 调用。
// All imports
import ...
loadMap(Highcharts);
loadDrilldown(Highcharts);
boost(Highcharts);
Vue.config.productionTip = false
Vue.use(VueCookie);
Vue.use(ElementUI, {locale});
Vue.use(VueRouter);
Vue.use(VueHighcharts, {Highcharts });
Vue.use(HighMaps);
// This is a global component declaration
Vue.component('app-oven', Devices);
Vue.component('app-sidebar', SideBar);
Vue.component('app-header', Header);
Vue.component('app-footer', Footer);
Vue.component('app-query', Query);
Vue.component('app-deviceproperties', DeviceProperties);
Vue.component('app-device', Device)
Vue.component('app-queryselection', QuerySelection)
Vue.component('app-index', Index)
Vue.component('app-index', Error)
Vue.component('app-realtime', RealTime);
Vue.component('app-login', Login)
Vue.component('app-preferences', Preferences)
const routes = [
{ path: '/index', component: Index},
{ path: '/', component: Login},
{ path: '/device/:deviceId', component: Device},
{ path: '/preferences', component: Preferences},
{ path: '*', component: Error}
];
const router = new VueRouter({
routes: routes,
mode: "history" // Gets rid of the # before the path
})
new Vue({
el: '#app',
router: router,
components: { App },
template: '<App/>'
})
后端是在 Node.js 上使用 Express 编写的,它响应来自前端的特定 AJAX 调用>.
// All imports
import ...
function prepareApp() {
let app = new Express();
app.use(cors({
origin: "*",
allowedHeaders: "Content-type",
methods: "GET,POST,PUT,DELETE,OPTIONS" }));
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();
});
app.use(helmet());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
// Get all parameters
app.get('/params', params.parameters);
// Get all devices ever seen on the databases
app.get('/devices', params.devices);
app.get('/organizeData', organizer.updateAll);
// WebApp used services to access various things
app.post('/customQuery', stats.query);
app.post('/statistics', stats.statistics)
app.post('/getUserInfo', stats.getUserInfo)
app.post('/setUserInfo', stats.setUserInfo)
app.post('/genericQuery', stats.genericQuery)
app.post('/NOSQLQuery', stats.NOSQLQuery)
// Users check and insertion
app.get('/insertUser', stats.insertUser)
app.post('/verifyUser', stats.verifyUser)
app.get('/', errors.hello); // Returns a normal "hello" page
app.get('*', errors.error404); // Catch 404 and forward to error handler
app.use(errors.error); // Other errors handler
return app;
}
let app = prepareApp();
//App listener on localhost:8080
app.listen(8080, () => {
console.log("App listening on http://localhost:8080");
});
我只在开发过程中使用了这个设置,所以我让 both 在 localhost 上同时运行,两者使用不同的端口。现在我想开始制作周期,但我不知道从哪里开始。
最重要的是,我将这两个应用程序部署到一个在外部服务器上运行的V虚拟M机器上。它已经有一个 DNS 关联和一个静态 IP 地址,因此已经涵盖了。 当我尝试在这台生产机器上同时运行这两个程序时出现问题,因为它的开放端口只有端口 80 和端口 443。我认为这在生产环境中很正常,但我不知道如何调整我的应用程序,以便它们仍然可以相互通信并从数据库中检索有用信息同时仍然使用单个端口 .
我希望我能很好地解释这个问题。期待一个好的(也许很长的)答案。
【问题讨论】:
-
你可以运行它 MEAN 风格,并让 nodejs 服务器为 Vue 应用程序提供服务
标签: node.js express vue.js production