Reputation: 31
I'm new to nodejs/expressjs. Could someone please explain how to to serve a page over https?
I have to ask this question another way, stackoverflow is complaining that my post is mainly code?
Here is the error dump:
app.get('/', function(request, response) {
^
TypeError: Object # has no method 'get' at Object. (/home/john/startup/docm/w2.js:21:5) 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) at startup (node.js:119:16) at node.js:901:3
And here is the code:
var express = require('express');
var fs = require('fs');
var app = express();
var options = {
ca: fs.readFileSync('csr.pem'),
cert: fs.readFileSync('cert.pem'),
key: fs.readFileSync('key.pem')
};
var server = require('https').createServer(options);
var portNo = 8889;
var app = server.listen(portNo, function() {
console.log((new Date()) + " Server is listening on port " + 8888);
});
app.get('/', function(request, response) {
app.use(express.static(__dirname));
console.log('app.get slash');
var buf = new Buffer(fs.readFileSync('index1.html'), 'utf-8');
response.send(buf.toString('utf-8'));
});
I'm new to nodejs/expressjs. Could someone please explain how to to serve a page over https?
Upvotes: 3
Views: 1937
Reputation: 91799
The problem with your application is that you're overriding your Express instance with your HTTPS instance. This is how it is properly done:
var fs = require('fs');
var express = require('express');
var app = express();
var https = require('https');
var options = {
ca: fs.readFileSync('csr.pem'),
cert: fs.readFileSync('cert.pem'),
key: fs.readFileSync('key.pem')
};
var server = https.createServer(options, app);
server.listen(443, function() {
console.log((new Date()) + ' Server is listening on port 443');
});
app.use(express.static(__dirname));
app.get('/', function(req, res) {
console.log('app.get slash');
var file = fs.readFileSync('index1.html', {encoding: 'utf8'});
res.send(file);
});
These were the errors in your code:
static()
middleware should be served outside of specific request handlers.readFileSync()
already has an encoding option.Upvotes: 3