2016-11-23 4 views
0

私は羽を学んでおり、問題があります。私は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()); 
}); 

このようにしてもOKですが、URLに元のパスが必要です。 URLはファイル名のようなパスを持つ必要があります。 次に、私がファイル: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()でファイルを使用すると、ファイルが存在しないときにエラー404を表示する方法はありますか?

3)私はファイルまたは混合ファイルを提供したいそれぞれのファイルにconst pathを使用する必要がありますか? (例えばmiddleware/index.jshereがそうでなければあなたはいつもはあなたが前にエラーハンドラをあなたのミドルウェアを含める - ちょうどエクスプレスのように -

答えて

0

羽ではなく、あなたがいることを確認してい生成されるアプリケーションでは動作しない理由はありません404を取得する

+0

今、それは動作します、ありがとう:-) – SeaDog

+0

私はまだ1つの質問があります。/src/middlewareのindex.jsにapp.get()を追加した場合、どのように/ publicディレクトリからファイルをインクルードできますか? – SeaDog

+0

'const path = require( 'path')'と 'const filename = path.join(__ dirname、 '..'、 '..'、 'public'、 'index.html')のファイルで' – Daff

関連する問題