服务器上通过http请求的url必须是绝对服务器渲染
URLs requested via http on the server must be absolute server rendering
我正在使用 Angular 4 CLI + Universal,但出现以下错误。我记得它也应该使用相对路径,但我该怎么做呢?
ERROR Error: URLs requested via Http on the server must be absolute.
URL: /api/countNewsFeedToday
at validateRequestUrl (/Users/Picchu/Documents/em/node_modules/@angular/platform-server/bundles/platform-server.umd.js:99:15)
HTTP请求调用示例[=21=]
ngOnInit() {
this.http.get('/isUserLogin').map((res: any) => res.json())
.subscribe(
(res: any) => {
this.data = res;
if (res == 'notLoggedIn') {
} else {
this.profileId = this.data._id;
if(this.data.local){
this.nameMain = this.data.local.nameMain;
}
}
});
}
Package.json
{
"name": "smarter-experiences",
"version": "1.0.0",
"description": "Starter application of Angular2 with Express",
"main": "dist/server/bin/www.js",
"license": "MIT",
"scripts": {
"start": "ng serve --proxy-config proxy.conf.json & npm run _server:run",
"lint": "tslint \"src/**/*.ts\"",
"test": "ng test",
"pree2e": "webdriver-manager update",
"e2e": "protractor",
"build": "ng build --prod --sm=false --aot --output-path=dist/client && npm run _server:build",
"_server:run": "tsc -p ./server && concurrently \"tsc -w -p ./server\" \"nodemon dist/server/bin/www.js\" ",
"_server:build": "tsc -p ./server",
"production": "NODE_ENV=production node dist/server/bin/www.js",
"buildWebpack": "ng build --prod && ngc && webpack",
"startWebpack": "node dist/server.js"
},
"private": true,
"dependencies": {
"@angular/animations": "^4.3.1",
"@angular/cli": "^1.2.4",
"@angular/common": "^4.3.1",
"@angular/compiler": "^4.3.1",
"@angular/compiler-cli": "^4.3.1",
"@angular/core": "^4.3.1",
"@angular/forms": "^4.3.1",
"@angular/http": "^4.3.1",
"@angular/material": "^2.0.0-beta.7",
"@angular/platform-browser": "^4.3.1",
"@angular/platform-browser-dynamic": "^4.3.1",
"@angular/platform-server": "^4.3.1",
"@angular/router": "^4.3.1",
"@types/body-parser": "0.0.32",
"@types/compression": "0.0.33",
"@types/cors": "0.0.33",
"@types/express": "^4.0.32",
"@types/jasmine": "2.5.38",
"@types/jsonwebtoken": "^7.1.31",
"@types/node": "^6.0.42",
"@types/socket.io": "^1.4.27",
"@types/socket.io-client": "^1.4.29",
"@types/uuid": "3.3.15-alpha",
"angular2-material-datepicker": "^0.5.0",
"angular2-moment": "^1.3.0",
"async": "~1.5.2",
"bcrypt-nodejs": "latest",
"body-parser": "~1.13.2",
"bootstrap": "^3.3.7",
"compression": "^1.6.2",
"connect-busboy": "0.0.2",
"connect-flash": "~0.1.1",
"cookie-parser": "~1.3.5",
"cookie-session": "2.0.0-alpha.2",
"core-js": "^2.4.1",
"cors": "^2.8.4",
"debug": "^2.2.0",
"express": "^4.13.4",
"express-jwt": "^3.3.0",
"express-session": "~1.0.0",
"font-awesome": "^4.7.0",
"jquery": "^3.1.1",
"json-schema": "^0.2.3",
"jsonwebtoken": "^5.7.0",
"method-override": "~1.0.0",
"mkdirp": "~0.5.1",
"mongoose": "^4.6.0",
"morgan": "^1.7.0",
"mosca": "*",
"mqtt": "*",
"multer": "^0.1.8",
"ng2-mqtt": "0.1.2",
"node-geocoder": "3.15.0",
"nodemailer": "4.1.0",
"nodemailer-smtp-transport": "^2.7.4",
"passport": "^0.4.0",
"passport-facebook": "^2.1.1",
"passport-google-oauth": "^0.1.5",
"passport-http-bearer": "^1.0.1",
"passport-local": "~1.0.0",
"path": "^0.12.7",
"reflect-metadata": "^0.1.10",
"rxjs": "5.4.1",
"semantic-ui-loader": "^2.2.3",
"socket.io": "^1.7.2",
"stripe": "^4.15.0",
"style-loader": "^0.19.0",
"system-sleep": "^1.3.0",
"ts-helpers": "^1.1.1",
"uuid": "^3.0.1",
"webpack": "~3.8.1",
"webpack-node-externals": "^1.6.0",
"webpack-sources": "^1.0.1",
"zone.js": "^0.8.9"
},
"devDependencies": {
"codelyzer": "~2.0.0",
"concurrently": "^3.1.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-coverage-istanbul-reporter": "^0.2.0",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-remap-istanbul": "^0.2.1",
"markerwithlabel": "^2.0.0",
"nodemon": "^1.11.0",
"protractor": "~5.1.0",
"ts-node": "~2.0.0",
"ts-loader": "^3.0.2",
"tslint": "~4.4.2",
"typescript": "~2.3.4",
"webdriver-manager": "10.2.5"
}
}
如错误所述,服务器端请求的 URL 必须是绝对的。它不适用于亲戚 URL。
一个好的解决方案是使用Angular Http Interceptors拦截服务器端的所有http请求并使它们像http://localhost:PORT/isUserLogin
一样绝对化。
我正在使用 Angular 4 CLI + Universal,但出现以下错误。我记得它也应该使用相对路径,但我该怎么做呢?
ERROR Error: URLs requested via Http on the server must be absolute. URL: /api/countNewsFeedToday at validateRequestUrl (/Users/Picchu/Documents/em/node_modules/@angular/platform-server/bundles/platform-server.umd.js:99:15)
HTTP请求调用示例[=21=]
ngOnInit() {
this.http.get('/isUserLogin').map((res: any) => res.json())
.subscribe(
(res: any) => {
this.data = res;
if (res == 'notLoggedIn') {
} else {
this.profileId = this.data._id;
if(this.data.local){
this.nameMain = this.data.local.nameMain;
}
}
});
}
Package.json
{
"name": "smarter-experiences",
"version": "1.0.0",
"description": "Starter application of Angular2 with Express",
"main": "dist/server/bin/www.js",
"license": "MIT",
"scripts": {
"start": "ng serve --proxy-config proxy.conf.json & npm run _server:run",
"lint": "tslint \"src/**/*.ts\"",
"test": "ng test",
"pree2e": "webdriver-manager update",
"e2e": "protractor",
"build": "ng build --prod --sm=false --aot --output-path=dist/client && npm run _server:build",
"_server:run": "tsc -p ./server && concurrently \"tsc -w -p ./server\" \"nodemon dist/server/bin/www.js\" ",
"_server:build": "tsc -p ./server",
"production": "NODE_ENV=production node dist/server/bin/www.js",
"buildWebpack": "ng build --prod && ngc && webpack",
"startWebpack": "node dist/server.js"
},
"private": true,
"dependencies": {
"@angular/animations": "^4.3.1",
"@angular/cli": "^1.2.4",
"@angular/common": "^4.3.1",
"@angular/compiler": "^4.3.1",
"@angular/compiler-cli": "^4.3.1",
"@angular/core": "^4.3.1",
"@angular/forms": "^4.3.1",
"@angular/http": "^4.3.1",
"@angular/material": "^2.0.0-beta.7",
"@angular/platform-browser": "^4.3.1",
"@angular/platform-browser-dynamic": "^4.3.1",
"@angular/platform-server": "^4.3.1",
"@angular/router": "^4.3.1",
"@types/body-parser": "0.0.32",
"@types/compression": "0.0.33",
"@types/cors": "0.0.33",
"@types/express": "^4.0.32",
"@types/jasmine": "2.5.38",
"@types/jsonwebtoken": "^7.1.31",
"@types/node": "^6.0.42",
"@types/socket.io": "^1.4.27",
"@types/socket.io-client": "^1.4.29",
"@types/uuid": "3.3.15-alpha",
"angular2-material-datepicker": "^0.5.0",
"angular2-moment": "^1.3.0",
"async": "~1.5.2",
"bcrypt-nodejs": "latest",
"body-parser": "~1.13.2",
"bootstrap": "^3.3.7",
"compression": "^1.6.2",
"connect-busboy": "0.0.2",
"connect-flash": "~0.1.1",
"cookie-parser": "~1.3.5",
"cookie-session": "2.0.0-alpha.2",
"core-js": "^2.4.1",
"cors": "^2.8.4",
"debug": "^2.2.0",
"express": "^4.13.4",
"express-jwt": "^3.3.0",
"express-session": "~1.0.0",
"font-awesome": "^4.7.0",
"jquery": "^3.1.1",
"json-schema": "^0.2.3",
"jsonwebtoken": "^5.7.0",
"method-override": "~1.0.0",
"mkdirp": "~0.5.1",
"mongoose": "^4.6.0",
"morgan": "^1.7.0",
"mosca": "*",
"mqtt": "*",
"multer": "^0.1.8",
"ng2-mqtt": "0.1.2",
"node-geocoder": "3.15.0",
"nodemailer": "4.1.0",
"nodemailer-smtp-transport": "^2.7.4",
"passport": "^0.4.0",
"passport-facebook": "^2.1.1",
"passport-google-oauth": "^0.1.5",
"passport-http-bearer": "^1.0.1",
"passport-local": "~1.0.0",
"path": "^0.12.7",
"reflect-metadata": "^0.1.10",
"rxjs": "5.4.1",
"semantic-ui-loader": "^2.2.3",
"socket.io": "^1.7.2",
"stripe": "^4.15.0",
"style-loader": "^0.19.0",
"system-sleep": "^1.3.0",
"ts-helpers": "^1.1.1",
"uuid": "^3.0.1",
"webpack": "~3.8.1",
"webpack-node-externals": "^1.6.0",
"webpack-sources": "^1.0.1",
"zone.js": "^0.8.9"
},
"devDependencies": {
"codelyzer": "~2.0.0",
"concurrently": "^3.1.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-coverage-istanbul-reporter": "^0.2.0",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-remap-istanbul": "^0.2.1",
"markerwithlabel": "^2.0.0",
"nodemon": "^1.11.0",
"protractor": "~5.1.0",
"ts-node": "~2.0.0",
"ts-loader": "^3.0.2",
"tslint": "~4.4.2",
"typescript": "~2.3.4",
"webdriver-manager": "10.2.5"
}
}
如错误所述,服务器端请求的 URL 必须是绝对的。它不适用于亲戚 URL。
一个好的解决方案是使用Angular Http Interceptors拦截服务器端的所有http请求并使它们像http://localhost:PORT/isUserLogin
一样绝对化。