我正在学习羽毛,我有一个问题。我试图做类似于PHP的开关的文件包含。如何使用feathers.js将文件包含到index.html中?
例如:
/src/middleware/index.js
'use strict';
const handler = require('feathers-errors/handler');
const notFound = require('./not-found-handler');
const logger = require('./logger');
const cheerio = require('cheerio')
const fs = require('fs');
const path = require('path')
const filename = path.join(__dirname, '..', '..', 'public')
module.exports = function() {
// Add your custom middleware here. Remember, that
// just like Express the order matters, so error
// handling middleware should go last.
const app = this;
app.get('/record.html', function(req, res) {
var html = fs.readFileSync(filename + '/index.html');
var home = fs.readFileSync(filename + '/record.html');
var $ = cheerio.load(html);
$('#content').html(home);
res.send($.html());
});
app.use(notFound());
app.use(logger(app));
app.use(handler());
};
我纠正了我的文件。我确信自己在写作的时候会做,但不幸的是我有一个问题。当我打开http://127.0.0.1:3030/record.html时,我只收到record.html,没有混合文件。如果我改变records.html上record.html的路径,例如
app.get('/records.html', function(req, res) {
var html = fs.readFileSync(filename + '/index.html');
var home = fs.readFileSync(filename + '/record.html');
var $ = cheerio.load(html);
$('#content').html(home);
res.send($.html());
});
这样可以,但我想在URL中有原始路径。 URL必须具有文件名称之类的路径。 反过来,如果我添加:file改为records.html,如果文件不存在,我会收到错误“Oh no!”而不是404
例如:
app.get('/:file.html', function(req, res) {
var file = req.params.file
var html = fs.readFileSync(filename + '/index.html');
var home = fs.readFileSync(filename + '/' + file + '.html');
var $ = cheerio.load(html);
$('#content').html(home);
res.send($.html());
});
然而一个问题。
const path = require('path')
const filename = path.join(__dirname, '..', '..', 'public')
如果app.js文件是const的路径,我必须把上面的代码中的每个文件中像中间件或服务时,我想从公共目录服务文件?我不能在这个应用程序中的所有文件使用全局变量?
app.js
'use strict';
const path = require('path'); <-- HERE const path
const serveStatic = require('feathers').static;
const favicon = require('serve-favicon');
const compress = require('compression');
const cors = require('cors');
const feathers = require('feathers');
const configuration = require('feathers-configuration');
const hooks = require('feathers-hooks');
const rest = require('feathers-rest');
const bodyParser = require('body-parser');
const socketio = require('feathers-socketio');
const middleware = require('./middleware');
const services = require('./services');
const app = feathers();
app.configure(configuration(path.join(__dirname, '..')));
app.use(compress())
.options('*', cors())
.use(cors())
.use(favicon(path.join(app.get('public'), 'favicon.ico')))
.use('/', serveStatic(app.get('public')))
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(hooks())
.configure(rest())
.configure(socketio())
.configure(services)
.configure(middleware);
module.exports = app;
1)我怎么能显示出与混合文件与网页文件名的路径例如http://127.0.0.1:3030/record.html
2)如果我使用:app.get()中的文件当文件不存在时如何显示错误404?
3)我是否必须在每个文件中使用const路径,在这里我想提供一个文件或混合文件?
太棒了!现在它的工作,谢谢:-) – SeaDog
我还有一个问题。如果我将app.get()添加到/ src/middleware中的index.js中,如何从/ public目录中包含文件? – SeaDog
在'const path = require('path')'和'const filename = path.join(__ dirname,'..','..','public','index.html')那个文件中' – Daff