我想使用node.js(v0.8.25)和Express(v3.3.1)建立一个web服务,服务器和客户端都运行node.js.我要求连接是安全的,并且客户端和服务器都可以相互验证。当我运行下面的代码时,我收到以下错误:
{ [Error: socket hang up] code: 'ECONNRESET' }
这是我的服务器代码:
var https = require('https');
var express = require('express');
var fs = require('fs');
var server = express();
server.configure(function(){
server.use(express.bodyParser());
});
// Listen to posts to 127.0.0.1/myroute
server.post('/myroute', function (req, res) {
// Do some fun stuff, error checking etc
res.writeHead(200, {'Content-Type': 'text/json'});
res.end("{Okay}");
});
var credentials = {
key: fs.readFileSync('../ssl/server.key'),
cert: fs.readFileSync('../ssl/server.crt'),
ca: fs.readFileSync('../ssl/ca.crt'),
requestCert: false,
rejectUnauthorized: false,
passphrase: 'test21'
};
var https_server = https.createServer(credentials, server);
server.listen(1337);
console.log('Server running at https://127.0.0.1:1337');
以下是客户端代码:
var fs = require('fs');
var https = require('https');
var options = {
hostname: '127.0.0.1',
port: 1337,
path: '/myroute',
method: 'GET',
key: fs.readFileSync('../ssl/client.key'),
cert: fs.readFileSync('../ssl/client.crt'),
ca: fs.readFileSync('../ssl/ca.crt'),
passphrase: 'test21'
};
https.get(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
}).on('error', function(e) {
console.error(e);
});