【问题标题】:node server only loads on safari localhost and not chrome's节点服务器仅在 safari localhost 而不是 chrome 上加载
【发布时间】:2019-07-09 17:03:41
【问题描述】:

使用http://localhost:8000 的节点服务器仅适用于 Safari 而不是 Chrome。使用 127.0.0.1:8000 可以在 safari 和 chrome 上运行。我很困惑为什么在我的 chrome 浏览器中使用 localhost 不起作用。 输入我的终端 ping localhost 并得到 127.0.0.1

在更新到 Angular 8 之前,我的项目运行良好,并且能够在我的本地主机上运行它。更新后我遇到了一些问题,所以我决定降级回 Angular 7。从那时起,我只能使用我的 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

Error page Error network page

【问题讨论】:

  • 运行ng serve时会发生什么?
  • 它会在 Chrome 中自动打开,但是当我在 safari 中打开它时它可以正常工作。
  • 所以没有错误?什么问题?
  • 错误请求,400 仅在 chrome 上,Safari 没有
  • 可以发截图吗?

标签: node.js angular npm angular7


【解决方案1】:

如果你的应用是Node JS:-

从您的 server.js 代码看起来您​​有 Node JS 应用程序。不是 Angular 应用程序。我在这里看不到任何组件。只需按 F5 看看会发生什么?

按 F5 后,打开 chrome 并输入 http://localhost:8000。它应该从你的 server.js 调用你的 get 方法。

如果您的应用程序是“Angular”:-

能否在 Visual Studio Code 中添加 Debugger for 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}"
        }
    ]
}

试试这个。希望它应该适用于您的场景。

【讨论】:

  • @Pushkar很棒的调试器扩展。所以它说:``` Angular 正在开发模式下运行。调用 enableProdMode() 以启用生产模式。 core.js:16829 ``` 这个项目一直在开发模式下运行所以我很困惑。
  • 这只是一个指令。您无需切换到生产。您可以通过更改 env.json 文件中的环境来切换到生产环境(如果有)。顺便说一句,您能否在 chrome 中看到您的 Angular 应用程序?
  • 您还可以检查 main.ts 文件代码下的 enableProduction 选项,例如 if (environment.production) { enableProdMode();}。还有文件夹环境。启用禁用生产模式的文件 environment.ts。
  • 是的,我能够看到我的 Angular 应用程序在 chrome 上运行,就像我想要的那样
  • 您认为可能是某个版本的节点或 angular/cli 不支持其中之一吗?
【解决方案2】:

感谢@Dave E,我浏览了旧帖子并终于弄明白了。他的answer 解释了一切

【讨论】:

    猜你喜欢
    • 2017-01-06
    • 2015-01-17
    • 1970-01-01
    • 2017-10-23
    • 2014-02-09
    • 2017-01-06
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    相关资源
    最近更新 更多