如何在express上配置ssl

时间:2014-12-17 12:11:51

标签: node.js ssl express

我正在尝试使用以下配置在express中配置ssl,但我一直在收到错误,我无法弄清楚如何解决这个问题。

 use strict';

//dependencies
var config = require('./config'),
    //uuid = require('node-uuid'),
    express = require('express'),
    session = require('express-session'),
    mongoStore = require('connect-mongo')(session),
    http = require('http'),
    path = require('path'),
    passport = require('passport'),
    mongoose = require('mongoose'),
    helmet = require('helmet'),
    https = require('https');


//create express app
var app = express();


//keep reference to config
app.config = config;


app.server = https.createServer(app.config.ssl_options, app);


//listen u
app.server.listen(443);

以下是我导入配置文件的方法,配置文件放在config.js

exports.ssl_options = {
  key: fs.readFileSync('./ssl.key'),
  cert: fs.readFileSync('./2b62bb384fb2b9.crt'),
  ca: fs.readFileSync('./gd_bundle-g2-g1.crt')
};

这是我的错误

    events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EACCES
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1023:19)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at Object.<anonymous> (/home/kseguy/node_projects/prwrite/app.js:100:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

我收到错误的行是

//listen u
app.server.listen(443);

1 个答案:

答案 0 :(得分:0)

如果你绑定到一个端口&lt; 1024,您必须使用sudo启动服务器,因为只有特权用户(root)才能绑定到这些端口。