我正在修改this GitHub sample app以使用Express代替KOA。我在CentOS 7终端中键入gulp serve
时启动应用程序,但是当我在浏览器中键入http : // localhost : 8080
时,会出现404错误消息:
Cannot GET /
到目前为止,我对代码所做的唯一三项更改是server/index.js
,router.js
和package.json
。到目前为止的所有变化总结如下:
服务器/ index.js 要替换
server/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
是:
{
"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": "soos.gabor86@gmail.com"
},
"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"
}
}
需要对代码进行哪些具体更改才能解决此404错误?
答案 0 :(得分:0)
您忘记在应用程序中实际使用路由器:
app.use(router.routes());
在您需要之后。