我正在使用Express Static节点服务器并具有以下server.js代码:
var express = require('express');
var app = express();
var fs = require('fs');
var publicdir = __dirname + '/client';
app.set('port', 8080);
app.use(function(req, res, next) {
if (req.path.indexOf('.') === -1) {
var file = publicdir + req.path + '.html';
fs.exists(file, function(exists) {
if (exists)
req.url += '.html';
next();
});
} else {
next();
}
});
app.use(express.static(publicdir));
// Listen for requests
var server = app.listen(app.get('port'), function() {
var port = server.address().port;
console.log('Magic happens on port ' + port);
});
我正在修剪文件名中的.html
以清理网址(例如mysite.com/blog-article-title
)。我想做的最后一步是制作它,以便在URL中添加一个尾随的“/”,但我不知道该怎么做。 (例如,mysite.com/blog-article-title/
)。
答案 0 :(得分:1)
var express = require('express');
var app = express();
var fs = require('fs');
var path = require('path');
app.set('port', 3001);
app.use(express.static(path.join(__dirname, 'client')));
app.get('/blog-article-title/', function(req, res) {
res.sendFile("blog-article-title.html");
})
var server = app.listen(app.get('port'), function() {
var port = server.address().port;
console.log('Magic happens on port ' + port);
});