带有Web浏览器Cors错误的Node.js服务器

时间:2018-06-26 16:42:03

标签: node.js

const express = require('express')
const app = express()
const port = 3000

app.all('*', function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  next();
});

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Credentials", "true");
    res.header("Access-Control-Allow-Headers", "Origin,Content-Type, Authorization, x-id, Content-Length, X-Requested-With");
    res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
    next();
});

app.get('/', (request, response) => {
  response.send('Hello from Express!')
  console.log("hi");
})

app.use('/', (request, response) => {
  response.send('Hello from Express!')
  console.log("hi");
})

app.listen(port, (err) => {
  if (err) {
    return console.log('something bad happened', err)
  }

  console.log(`server is listening on ${port}`)
})

这是我的node.js服务器代码

连接网页时,我从f12的“网络”标签中收到

https://localhost:3000 http connect 200(已建立连接)

但是,我的node.js应用程序未输出任何内容。有什么建议吗?

注意,我使用了提琴手并收到了此回复

  

与本地主机(对于#125)的HTTPS握手失败。 System.IO.IOException由于意外的数据包格式,握手失败。

1 个答案:

答案 0 :(得分:0)

在您的问题中,您正在请求中使用https。除非您在其他地方(例如在nginx之后)配置了SSL,否则它将无法正常工作。您的应用程序中没有处理安全连接的内容。将其更改为http,它应该可以工作。