2016-11-23 5 views
0

나는 깃털을 배우고 있고 나는 문제가있다. 나는 PHP의 스위치와 비슷한 파일 포함을 시도한다. 예를 들어feathers.js를 사용하여 index.html에 파일을 포함시키는 방법은 무엇입니까?

:

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

/src/middleware/index.js가 내 파일을 수정했습니다. 나는 당신이 글을 쓰는 동안 내가하고있는 일을 확실히하고, 불행히도 나는 문제가있다. 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에는 파일 이름과 같은 경로가 있어야합니다. 차례로, 내가 파일에 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()); 
}); 

그리고 또 하나 개의 질문. app.js 파일에 CONST 경로 인 경우

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

, 나는 공용 디렉토리에서 파일을 제공 할 때 미들웨어 또는 서비스와 같은 각 파일에 위의 코드를 삽입해야? 이 응용 프로그램의 모든 파일에 전역 변수를 사용할 수 없습니까?

내가 예를 들어, 파일 이름의 경로로 혼합 된 파일을 사용하여 페이지를 표시 할 수 있습니까

'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

)을 app.js http://127.0.0.1:3030/record.html

2) app.get()에서 파일을 사용하는 경우 파일이 존재하지 않을 때 404 오류를 표시하는 방법은 무엇입니까?

3) 파일 또는 혼합 파일을 제공하려는 각 파일에 const path를 사용해야합니까? IN (예 : middleware/index.jshere 그렇지 않으면 당신은 항상 것입니다 당신이 전에 오류 처리기를 미들웨어 을 포함한다 - 단지 익스프레스처럼 -

답변

0

깃털이 아니라 당신이 있는지 확인이 생성 된 응용 프로그램에서 작동하지 않을 이유가 없다 404.

+0

이제 덕분에, 덕분에 :-) – SeaDog

+0

나는 아직 하나의 질문이있다./src/middleware의 index.js에 app.get()을 추가하면 어떻게/public 디렉토리의 파일을 포함시킬 수 있습니까? – SeaDog

+0

'const path = require ('path ')'와'const filename = path.join (__ dirname,'.. ','.. ','public ','index.html ') 파일에서 – Daff