2016-07-27 6 views
0

this GitHub sample appは、KOAの代わりにExpressを使用するように修正しています。私はCentOSの7端子にgulp serveを入力すると、アプリが起動し、私は、ブラウザでhttp : // localhost : 8080を入力すると、メッセージを404のエラー結果:Angular2とExpress.jsでGET/inできない

Cannot GET/

私はこれまでコードに加えられた3つのだけ変更がしましたserver/index.js,router.jsおよびpackage.jsonである。 server/index.jsを置き換えるために新しいコードがある


サーバー/ index.js
:次のようにすべての変更は、これまでに要約されている

// set up ====================================================================== 
var express = require('express'); 
var app  = express();        // create our app w/ express 
var port  = process.env.PORT || 8080;    // set the port 
var morgan = require('morgan');    // log requests to the console (express4) 
var bodyParser = require('body-parser'); // pull information from HTML POST (express4) 
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4) 

app.use(express.static(__dirname + '/public'));     // set the static files location /public/img will be /img for users 
app.use(morgan('dev'));           // log every request to the console 
app.use(bodyParser.urlencoded({'extended':'true'}));   // parse application/x-www-form-urlencoded 
app.use(bodyParser.json());          // parse application/json 
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json 
app.use(methodOverride()); 
app.use('/scripts', express.static(__dirname + '/node_modules/')); 

// load the routes 
require('./router'); 

// listen (start app with node server.js) ====================================== 
app.listen(port); 
console.log("App listening on port " + port); 


router.js
そして私は最後に次を追加しました router.jsのメッセージが表示されますが、コンソールに inside/routeというメッセージは表示されません。つまり、Expressルートが呼び出されていません。

router.get('*', function(req, res) { 
    console.log('inside/route!'); 
    res.sendfile('./client/index.html'); // load the single view file (angular will handle the front-end) 
}); 
新しい依存関係を反映する


package.json
新しい package.jsonは次のようになります。この404エラーを解決するために、コードになされる必要がある特定の

{ 
    "name": "angular2-esnext-starter", 
    "version": "1.0.0", 
    "main": "server/index.js", 
    "scripts": { 
    "start": "node server/index.js", 
    "test": "COVERAGE_ENABLED=true gulp test", 
    "webdriver-update": "webdriver-manager update" 
    }, 
    "repository": { 
    "type": "git", 
    "url": "git+https://github.com/blacksonic/angular2-esnext-starter.git" 
    }, 
    "author": { 
    "name": "blacksonic", 
    "email": "[email protected]" 
    }, 
    "license": "ISC", 
    "keywords": [ 
    "angular2", 
    "es6", 
    "webpack", 
    "gulp" 
    ], 
    "description": "Angular 2 development in Javascript with ES6/ES7 syntax powered by Babel.", 
    "engines": { 
    "node": "4.4.2" 
    }, 
    "dependencies": { 
    "express": "^4.13.4", 
    "jsonwebtoken": "7.0.1", 
    "morgan": "^1.1.1", 
    "method-override": "^2.1.3", 

    "node-uuid": "1.4.7" 
    }, 
    "devDependencies": { 
    "@angular/common": "2.0.0-rc.4", 
    "@angular/compiler": "2.0.0-rc.4", 
    "@angular/core": "2.0.0-rc.4", 
    "@angular/forms": "0.2.0", 
    "@angular/http": "2.0.0-rc.4", 
    "@angular/platform-browser": "2.0.0-rc.4", 
    "@angular/platform-browser-dynamic": "2.0.0-rc.4", 
    "@angular/router": "3.0.0-beta.2", 
    "babel-core": "6.10.4", 
    "babel-eslint": "6.1.1", 
    "babel-loader": "6.2.4", 
    "babel-plugin-__coverage__": "11.0.0", 
    "babel-preset-angular2": "0.0.2", 
    "babel-preset-es2015": "6.9.0", 
    "bootstrap": "3.3.6", 
    "codeclimate-test-reporter": "0.3.3", 
    "del": "2.2.1", 
    "es6-promise": "3.2.1", 
    "es6-shim": "0.35.1", 
    "gulp": "3.9.1", 
    "gulp-cssnano": "2.1.2", 
    "gulp-delete-lines": "0.0.7", 
    "gulp-eslint": "3.0.1", 
    "gulp-less": "3.1.0", 
    "gulp-nodemon": "2.1.0", 
    "gulp-protractor": "2.4.0", 
    "gulp-util": "3.0.7", 
    "gulp-watch": "4.3.8", 
    "jasmine-core": "2.4.1", 
    "json-loader": "0.5.4", 
    "karma": "1.1.1", 
    "karma-coverage": "1.1.0", 
    "karma-jasmine": "1.0.2", 
    "karma-phantomjs-launcher": "1.0.1", 
    "karma-sourcemap-loader": "0.3.7", 
    "karma-webpack": "1.7.0", 
    "localStorage": "1.0.3", 
    "mini-lr": "0.1.9", 
    "phantomjs-polyfill": "0.0.2", 
    "phantomjs-prebuilt": "2.1.7", 
    "protractor": "3.3.0", 
    "raw-loader": "0.5.1", 
    "reflect-metadata": "0.1.3", 
    "run-sequence": "1.2.2", 
    "rxjs": "5.0.0-beta.6", 
    "validate.js": "0.10.0", 
    "webpack": "1.13.1", 
    "zone.js": "0.6.12" 
    } 
} 

どのような変更?あなたがそれを必要とした後

app.use(router.routes()); 

+0

あなたは解決策を見つけることができましたか? – Danoram

答えて

0

は、あなたが実際のアプリケーションでルータを使用することを忘れていました。

+0

私は 'var router = require( './ router');を試しました。 app.use(router.routes()); '、しかしブラウザは何も返さずに保持パターンに詰まってしまいます。エラーはありません。失敗。何もない。あなたの提案は答えの一部に過ぎません。それ以外に何ができますか? – FirstOfMany

関連する問題