2017-05-09 3 views
2

Node.jsとExpressでAngular 2アプリを作成しています。

私の問題は、ルートファイルがワイルドカードで動作しないということです。 ReferenceError: path is not defined

マイserver.js:Node.js/Expressがすべて2番のページにリダイレクト

const express = require('express'); 
const app = express(); 
const path = require('path'); 
const routes = require('./routes'); 
const data = require('./articles.json'); 

app.use(express.static(path.join(__dirname, '/dist'))); 
app.use('/', routes); 

app.listen(8080, function() { 
    console.log('App started on port 8080'); 
}); 

マイ/routes/index.js:

const routes = require('express').Router(); 

routes.get('*', function (req, res) { 
    res.sendFile(path.join(__dirname + '/dist/index.html')); 
}); 

module.exports = routes; 
毎回私は、他のものとのページをご覧 /(たとえば /testのために)それは次のように述べてい

ここで私は何が間違っていますか?

答えて

1

あなたは私の神ああ、あまりにも

/routes/index.js

const path = require('path'); 
const routes = require('express').Router(); 

routes.get('*', function (req, res) { 
    res.sendFile(path.join(__dirname + '/dist/index.html')); 
}); 

module.exports = routes; 
+2

あなたのindex.jsにパスパッケージを必要とするように感謝を必要としています。私はすべてをルートファイルに移動しましたが、パスを忘れてしまいました。 Lol –

+0

@LuudvanKeulenは私たちの最高の出来栄えです:-) – echonax

+0

私は8分後にそれを受け入れます(SO Timerはすぐにそれを受け入れることはできません)。 –

関連する問題