# Node基础
创建一个http和https服务器:
'use strict'
var http = require('http');
var https = require('https');
var fs = require('fs');
var serveIndex = require('serve-index');
var express = require('express');
var app = express();
//顺序不能换
app.use(serveIndex('./public'));
app.use(express.static('./public'));
var options = {
key : fs.readFileSync('./cert.key'),
cert : fs.readFileSync('./cert.pem')
}
var https_server = https.createServer(options, app);
https_server.listen(443, '0.0.0.0');
var http_server = http.createServer(app);
http_server.listen(80, '0.0.0.0');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26