2017-03-18 7 views
2

私は基本的なMEANアプリケーションを実行しようとしています。私のアプリはAngularパーツなしで動作しています。 しかし、Angularを含めると、 "view"フォルダにあるHTMLページを見ることができません。これは、インデックスに私のルートは "ルート" ディレクトリの下にあるビューで自分のhtmlファイルが見えない

var express=require('express'); 
var path=require('path'); 
var bodyParser=require('body-parser'); 

var index=require('./routes/index'); 
var tasks=require('./routes/tasks'); 
var port=3000; 
var app=express(); 

/*View engine*/ 
app.set('views',path.join(__dirname, 'views')); 
app.set('view engine','ejs'); 
app.engine('html',require('ejs').renderFile); 

/*View static folder*/ 
app.use(express.static(path.join(__dirname, 'client'))); 

app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({extended: false})); 

app.use('/',index); 
app.use('/api',tasks); 
app.listen(port, function(){ 
    console.log('The application is running on port '+port); 
}); 

var express=require('express'); 
var router=express.Router(); 

router.get('/',function(req, res, next){ 
    res.render('index.html'); 
}) 
module.exports=router; 

私の角度のコードは、クライアント/ appディレクトリに住んでいる

は、これが私のサーバーです。 端末のmyapp/clientディレクトリにあるときにnpm startと入力しています。 「取得できません」と表示され、端末に「404 GET /index.html」が表示されます

アイデアはありますか?

答えて

3

これは、ときは、オープンルートルート(/ URIが)それはindex.htmlファイルにレンダリングすることを意味します:

router.get('/',function(req, res, next){ 
    res.render('index.html'); 
}); 

を、これは(あなたの質問index.htmlファイルで)あなたのビューファイルはviewsフォルダにあることを意味します

app.set('views',path.join(__dirname, 'views')); 

あなたはindex.htmlにはEJSによってレンダリングされるようにしたい場合は、そうviewsフォルダ(ないclientフォルダへ)へindex.htmlファイルを置きます。

と直接アプリを開きます。http://localhost:3000/


P.S.をあなたはこのようにindex.htmlをを開きたい場合:

あなたroutes/index.jsこのコンテンツを持っていることを確認してください:

const 
    express = require('express'), 
    router = express.Router(); 

const renderViewFile = (filename) => (req, res) => res.render(filename); 

router.get('/', renderViewFile('index.html')); 
router.get('/index.html', renderViewFile('index.html')); 

module.exports = router; 

か:put index.html file into client folder and it will be served as static file

+0

私のindex.htmlが下にあるhttp://localhost:3000/index.html

は、次の操作を行いますmyapp \ views \ myapp \ views \ clientではなく、まだ動作していません:( –

+0

@TuviaKhusid OK、 '/ index.html'を提供する追加ルートを追加します。実際にログにはファイルにアクセスしようとしていたブラウズからこのように 'http:// localhost:3000/index.html'のようにアクセスすることはできません。cuz route'/index.html'は普通のルートとしてexpressjsルータで処理されます。静的ミドルウェアは 'client'フォルダにあります – num8er

+0

@TuviaKhusidは私の最後に更新された答えをチェックし、それがあなたのニーズに合うことを願って – num8er

関連する問題