0

我正在学习羽毛,我有一个问题。我尝试进行类似于 PHP 切换的文件包含。

例如:

/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。如果我从 record.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 以防万一,如果文件不存在,我会收到错误“哦,不!” 而是 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 路径,当我想从公共目录提供文件时,我必须将上述代码放在中间件或服务等每个文件中?我不能对这个应用程序中的所有文件使用全局变量吗?

应用程序.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() 中使用 :file,当文件不存在时如何显示错误 404?

3) 我是否必须在要提供文件或混合文件的每个文件中使用 const 路径?

4

1 回答 1

0

没有理由在 Feathers 中不起作用,但是在生成的应用程序中,您必须确保 - 就像在 Express 中一样 -在错误处理程序之前包含中间件(例如,在middleware/index.js 这里,否则您将始终得到 404。

于 2016-11-23T16:37:51.397 回答