[英]Routing on NodeJS Express
I'm developing a server in NodeJS with Express, but the web structure is a little bit complicate (I didn't do it, and I can't change it either). 我正在使用Express在NodeJS中开发服务器,但是Web结构有点复杂(我没有这样做,也无法更改)。
The flow is something like this: 流程是这样的:
https://localhost/**{id}**
. https://localhost/**{id}**
类的呼叫。 index.html
. index.html
。 index.html
is or in folders, wherever the clients wanted to develop them. index.html
所在级别或文件夹中拥有更多视图。 The issue I'm having is how to route the files correctly. 我遇到的问题是如何正确路由文件。 Since I'm receiving the ID only when the index it's called, I couldn't figure out how to route links like
<a href="view1.html">View 1</a>
or even the javascript files calls <script src='scripts/someGreatFunctions.js'></script>
since they could also be in the root or in a folder (even the two things at the same time). 由于我仅在调用索引时才收到ID,因此我不知道如何路由诸如
<a href="view1.html">View 1</a>
类的链接,甚至无法路由javascript文件调用<script src='scripts/someGreatFunctions.js'></script>
因为它们也可以位于根目录或文件夹中(甚至同时位于两个目录中)。
My server.js
file: 我的
server.js
文件:
const express = require('express');
const https = require('https');
const fs = require('fs');
const app = express();
const config = require('./config');
var webId;
var options = {
key: fs.readFileSync(config.paths.certificate.key),
cert: fs.readFileSync(config.paths.certificate.crt),
requestCert: false,
rejectUnauthorized: false
};
app.use(function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, POST, PUT");
res.setHeader("Access-Control-Allow-Headers", "Accept, Access-Control-Allow-Headers, Access-Control-Request-Headers, Access-Control-Request-Method, Authorization, Content-Type, Origin, X-Requested-With");
next();
});
app.get('/scripts/:script', function(req, res) {
res.sendFile(req.params.script, {root: config.paths.webs + webId + '/scripts'});
});
app.get('/:script.js', function(req, res) {
res.sendFile(req.params.script + '.js', {root: config.paths.webs});
});
// This routes correctly the index
app.get('/:id', function(req, res) {
webId = req.params.id;
res.sendFile('index.html', {root: config.paths.webs + webId});
});
// This DID NOT work
app.get('/:id/:page', function(req, res) {
//Some code here...
});
https.createServer(options, app).listen(443, function() {
console.log("NodeJS secure server started at port 443");
});
I am also in a learning phase. 我也处于学习阶段。 Hope this will help.
希望这会有所帮助。
app.get('/test/:id/:page', function(req, res, next) {
let id = req.params.id;
let page = req.params.page;
console.log('The id: ' + id);
console.log('The page: ' + page);
});
I finally got the answer. 我终于得到了答案。 Seems like it's very important the order in which
gets
are declared. 似乎声明
gets
的顺序非常重要。 Also, I used a regular expression to make it a little more generic. 另外,我使用了一个正则表达式使它更加通用。
/**
* HTTP GET for files (.html, .js, etc.) from another folder level
*/
app.get('/:id/:folder/:file', function(req, res) {
if (typeof(webId) === undefined) {
webId = req.params.id;
}
let folder = req.params.folder;
let file = req.params.file;
res.sendFile(file, {root: config.paths.webs + webId + '/' + folder});
});
/**
* HTTP GET for .js files from the base path
*/
app.get(/\/\w+.\b(js)/, function(req, res) {
let lastBar = req.url.lastIndexOf('/');
let script = req.url.substr(lastBar);
res.sendFile(script, {root: config.paths.webs});
});
/**
* HTTP GET for index page
*/
app.get('/:id', function(req, res) {
webId = req.params.id;
res.sendFile('index.html', {root: config.paths.webs + webId});
});
/**
* HTTP GET for view from base path
*/
app.get('/:id/:page', function(req, res) {
if (typeof(webId) === undefined) {
webId = req.params.id;
}
let page = req.params.page;
res.sendFile(page, {root: config.paths.webs + webId});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.