简体   繁体   English

express.js不响应https发布

[英]express.js doesn't respond to https post

I'm trying to serve HTTPS and listen to POST request and it doesn't respond to the request. 我正在尝试提供HTTPS并监听POST请求,但它不响应该请求。

I'm using node 0.12.6 and express 4.13.3 我正在使用节点0.12.6并表示4.13.3

I think that the routing should be set differently but I'm not sure how.. 我认为路由应设置为不同,但我不确定如何。

var fs = require('fs');
var express = require('express');
var app = express();
var https = require('https');

var privateKey  = fs.readFileSync('my_key.key', 'utf8');
var certificate = fs.readFileSync('my_cert.crt', 'utf8');

https.createServer({key: privateKey, cert: certificate}).listen(3000);
app.post('/process', function(req, res) {
  res.send('processing...');
});

You should listen on app, not https. 您应该在应用程序而不是https上收听。

https isn't even required if you're using expressjs 如果您使用expressjs,甚至不需要https

var fs = require('fs');
var express = require('express');
var app = express();

var privateKey  = fs.readFileSync('my_key.key', 'utf8');
var certificate = fs.readFileSync('my_cert.crt', 'utf8');


app.post('/process', function(req, res) {
  // do something
}).listen(3000);

You are not passing your express app as an option to your https server after your credentials. 获得凭据后,您不会将快速app作为选项传递给https服务器。 So when this server is being hit with the post it doesn't know to use the express route to respond. 因此,当该服务器被帖子打中时,它不知道使用快递路线进行响应。 The following code block is from This question and is the same way that I implement https and is pretty easy to read/follow. 以下代码块来自“ 这个问题”,与我实现https的方式相同,并且很容易阅读/关注。

var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey  = fs.readFileSync('sslcert/server.key', 'utf8');
var certificate = fs.readFileSync('sslcert/server.crt', 'utf8');

var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

// your express configuration here

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(80);
httpsServer.listen(443);

Notice the app being passed as the second option in https.createServer(credentials, app); 请注意,该app是作为https.createServer(credentials, app);的第二个选项传递的https.createServer(credentials, app); You can do away with the httpServer if you want but doing it this way also allows you to handle http requests in whatever way that you want. 您可以根据需要取消使用httpServer,但是这样做也可以使您以所需的任何方式处理http请求。 This is useful if the user uses http instead of https in their address. 如果用户在地址中使用http而不是https,这将很有用。 You can then use the http server to redirect them to the secure https route. 然后,您可以使用http服务器将其重定向到安全的https路由。

If you continue to have CORS issues, try adding 如果您仍然遇到CORS问题,请尝试添加

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

to your express configurations. 到您的明确配置。 This will allow cross domain requests to your server 这将允许跨域请求到您的服务器

Thanks all, 谢谢大家

the problem was on client actually. 问题出在客户身上。 I got "connection refused" when doing xmlhttprequest to localhost . localhost执行xmlhttprequest时出现“连接被拒绝”的情况。 Altough it was the same server, when I used server name , problem solved. 虽然是同一台服务器,但是当我使用服务器名时,问题解决了。

As for CORS, i used CORS npm and 至于CORS,我使用了CORS npm和

app.use(cors());

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

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