I would like to set up a webservice using node.js (v0.8.25) and Express (v3.3.1), both server and client are running node.js. I require that the connection is secure and that both the client and server can authenticate each other. When I run the code below I get the following error:
{ [Error: socket hang up] code: 'ECONNRESET' }
Here is my server code:
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');
Here is the client code:
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);
});