使用http://localhost:8000的节点服务器只能在Safari中工作,而不能在Chrome中工作。使用127.0.0.1:8000在Safari和Chrome上均可使用。我感到困惑,为什么在chrome浏览器中无法使用localhost。 在我的终端ping localhost中输入并得到127.0.0.1
在更新到angular 8之前,我的项目运行正常,并且能够在本地主机上运行它。更新后我遇到了一些问题,因此我决定降级到angular7。从那时起,我只能使用IP地址和本地主机运行项目,而只能使用Safari,而不是Chrome。知道有什么问题或如何解决这个问题吗?
server.js
const express = require('express');
const app = express();
const path = require('path');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const session = require('express-session');
const mongooseDatabase = require("./server/config/mongoose");
const userRoutes = require('./server/config/routes/users');
const taskRoutes = require('./server/config/routes/tasks');
//Body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/dist/todo'));
app.use(session({
secret:"thisisasecret",
saveUninitialized:true,
resave:false,
cookie: {maxAge:1000}
}))
app.set('trust proxy', 1);
mongoose.connect(mongooseDatabase.database, { useCreateIndex: true, useNewUrlParser:true });
mongoose.connection.on('connected', () => {
console.log("Connected to database " + mongooseDatabase.database);
})
mongoose.connection.on('error', (err) => {
console.log("Database database " + err);
})
app.use('/users', taskRoutes, userRoutes);
taskRoutes(app);
userRoutes(app);
app.all("*", (req,res,next) => {
res.sendFile(path.resolve("./dist/todo/index.html"))
});
app.listen(8000, () => console.log("Running on port 8000"));
全球版本
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 7.3.4
Node: 10.16.0
OS: darwin x64
Angular:
...
Package Version
------------------------------------------------------
@angular-devkit/architect 0.13.4
@angular-devkit/core 7.3.4
@angular-devkit/schematics 7.3.4
@schematics/angular 7.3.4
@schematics/update 0.13.4
rxjs 6.3.3
typescript 3.2.4
package.json
{
"name": "todo",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~7.2.0",
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/forms": "~7.2.0",
"@angular/material": "^6.2.1",
"@angular/platform-browser": "~7.2.0",
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"angular2-flash-messages": "^3.0.1",
"body-parser": "^1.19.0",
"core-js": "^2.5.4",
"express": "^4.17.1",
"express-session": "^1.16.2",
"jquery": "^3.4.1",
"moment": "^2.24.0",
"mongoose": "^5.5.15",
"mongoose-unique-validator": "^2.0.3",
"path": "^0.12.7",
"rxjs": "~6.3.3",
"tslib": "^1.9.0",
"zone.js": "~0.8.26",
"@angular/cdk": "^6.2.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.13.0",
"@angular/cli": "~7.3.3",
"@angular/compiler-cli": "~7.2.0",
"@angular/language-service": "~7.2.0",
"@types/node": "~8.9.4",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "~4.5.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.2.2"
},
"description": "This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.3.3.",
"main": "index.js",
"keywords": [],
"author": "",
"license": "ISC"
}
如果您需要其他代码/问题lmk。
angular / cli本地项目版本
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 7.3.4
Node: 10.16.0
OS: darwin x64
Angular: 7.2.15
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.13.9
@angular-devkit/build-angular 0.13.9
@angular-devkit/build-optimizer 0.13.9
@angular-devkit/build-webpack 0.13.9
@angular-devkit/core 7.3.9
@angular-devkit/schematics 7.3.4
@angular/cdk 6.4.7
@angular/cli 7.3.4
@angular/material 6.2.1
@ngtools/webpack 7.3.9
@schematics/angular 7.3.4
@schematics/update 0.13.4
rxjs 6.3.3
typescript 3.2.4
webpack 4.29.0
答案 0 :(得分:0)
一个,如果您的应用程序是 Node JS :-
从server.js代码中获取似乎您具有Node JS应用程序。不是Angular应用程序。我在这里看不到任何组件。只需按F5键,看看会发生什么?
按F5后,打开镶边并键入http://localhost:8000。它应该从server.js调用get方法。
两个如果您的应用程序是“ Angular ”:-
您能否在Visual Studio代码中将 Chrome调试器作为扩展名添加?
添加后,您可以按F5在Chrome中打开应用程序。
按F5时,它将要求您选择目标Chrome,它将在您的项目中创建launch.js。然后在配置中添加网站网址。
以下是示例。
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8000",
"webRoot": "${workspaceFolder}"
}
]
}
尝试一下。希望它可以在您的情况下工作。
答案 1 :(得分:0)
我经历了一些旧帖子,终于感谢@DaveE。他的answer解释了一切