簡體   English   中英

如何使用feathers.js將文件包含到index.html?

[英]How to inclusion a file to index.html with feathers.js?

我正在學習羽毛,但有問題。 我嘗試進行類似於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而沒有混合文件。 如果我從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,以防萬一,如果文件不存在,我會收到錯誤消息“哦,不!”。 而是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()中使用:file,當文件不存在時如何顯示錯誤404?

3)是否必須在每個要提供文件或混合文件的文件中使用const路徑?

沒有理由在Feathers中不起作用,但是在生成的應用程序中,您必須確保-就像在Express中一樣-在錯誤處理程序之前包括中間件(例如,在此處的 middleware/index.js 否則始終會得到一個404。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM