简体   繁体   English

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

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

I'm learning a feathers and I have a problem. 我正在学习羽毛,但有问题。 I try to do file inclusion similar to switch of PHP. 我尝试进行类似于PHP开关的文件包含。

For example: 例如:

/src/middleware/index.js /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());
};

I corrected my file. 我更正了我的文件。 I made sure that I'm doing as you write, and unfortunately I have a problem. 我确定我在写信时正在做,但不幸的是我遇到了问题。 When I open http://127.0.0.1:3030/record.html I'm getting only record.html without mixed files. 当我打开http://127.0.0.1:3030/record.html时,我只得到了record.html而没有混合文件。 If I change path from record.html on records.html eg 如果我从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());
});

In this way is OK but I want to have original path in URL. 这样就可以了,但我想在URL中使用原始路径。 An URL must to have path like file name. URL必须具有类似文件名的路径。 In turn, if I add :file instead records.html in case, if the file does not exist I getting error "Oh no!" 反过来,如果我添加:file而不是records.html,以防万一,如果文件不存在,我会收到错误消息“哦,不!”。 instead 404. 而是404。

for example: 例如:

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());
});

And yet one question. 还有一个问题。

const path = require('path')
const filename = path.join(__dirname, '..', '..', 'public')

If in app.js file is const path, I have to put above code in each file like middleware or service when I want to serve file from public directory? 如果在app.js文件中是const路径,当我想从公共目录中提供文件时,是否必须将上述代码放在中间件或服务等每个文件中? I can not use a global variable for all files in this app? 我不能为该应用程序中的所有文件使用全局变量吗?

app.js 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) How can I show page with mixed file with the path of file name eg http://127.0.0.1:3030/record.html 1)如何显示带有文件名路径的混合文件页面,例如http://127.0.0.1:3030/record.html

2) If I use :file in app.get() how to show error 404 when a file not exists? 2)如果我在app.get()中使用:file,当文件不存在时如何显示错误404?

3) Do I have to use const path in each file wherein I want to serve a file or mixed files? 3)是否必须在每个要提供文件或混合文件的文件中使用const路径?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM