繁体   English   中英

在Angular2和Express.js中无法获取/

[英]Cannot GET / in Angular2 and Express.js

我正在修改此GitHub示例应用程序以使用Express而不是KOA。 当我在CentOS 7终端中键入gulp serve时,该应用程序启动,但是当我在浏览器中键入http : // localhost : 8080时,出现404错误并显示以下消息:

Cannot GET / 

到目前为止,我对代码所做的仅有三处更改是server/index.jsrouter.jspackage.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的底部添加了以下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错误,需要对代码进行哪些特定更改?

您忘记将路由器实际与应用程序一起使用:

app.use(router.routes());

在您需要它之后。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM