一隶糕、Node.js知識點(diǎn)
1.Node.js配置https
var express = require(“express”);
var app = express(); //使用nodejs自帶的http狈网、https模塊
var https = require('https');
var http = require('http');
var fs = require('fs'); //用于讀入證書文件
var path = require("path");
//根據(jù)項(xiàng)目的路徑導(dǎo)入生成的證書文件
var privateKey = fs.readFileSync(path.join(__dirname, '/public/215028913200048.key'), 'utf8');
var certificate = fs.readFileSync(path.join(__dirname, '/public/215028913200048.pem'), 'utf8');
var credentials = {key: privateKey, cert: certificate};
//創(chuàng)建http與HTTPS服務(wù)器
var httpServer = http.createServer(app,function () {
console.log('HTTP Server is running on: http://localhost:%s', PORT);
});
httpServer.listen(3000);
//創(chuàng)建http與HTTPS服務(wù)器
var httpsServer = https.createServer(credentials, app,function () {
console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT); }); //可以根據(jù)請求判斷是http還是https
app.get('/', function (req, res) {
if(req.protocol === 'https') {
res.status(200).send('This is https visit!');
} else {
res.status(200).send('This is http visit!');
}
});
httpsServer.listen(3001);
2.Node.js解決跨域問題
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1');
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
3.ajax訪問
- 一般情況下躏吊,使用微信小程序或Vue對后端進(jìn)行訪問時(shí)宴凉,都是需要https訪問才可以普舆,此時(shí)恬口,我們只需要對Node程序進(jìn)行https配置并且進(jìn)行跨域處理即可。當(dāng)然沼侣,配置方法上面已經(jīng)給出祖能。
示例代碼:
var express = require("express");
var app = express(); //使用nodejs自帶的http、https模塊
var https = require('https');
var fs = require('fs');
var path = require("path");
//根據(jù)項(xiàng)目的路徑導(dǎo)入生成的證書文件
var privateKey = fs.readFileSync(path.join(__dirname, '/public/215028913200048.key'), 'utf8');
var certificate = fs.readFileSync(path.join(__dirname, '/public/215028913200048.pem'), 'utf8');
var credentials = {key: privateKey, cert: certificate};
//創(chuàng)建http與HTTPS服務(wù)器
var httpsServer = https.createServer(credentials, app, function () {
console.log('HTTPS Server is running on: https://localhost:%s');
});
//設(shè)置跨域訪問
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1');
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
app.get('/',function (req, res) {
res.status(200).send('This is https visit!');
console.log("lalala");
});
app.get('/news', function (req, res) {
console.log("歷史性突破誕生啦");
})
httpsServer.listen(3000);
文集推薦:
Java基礎(chǔ)方法集1
Python基礎(chǔ)知識完整版
Spring Boot學(xué)習(xí)筆記
Linux指令進(jìn)階
Java高并發(fā)編程
SpringMVC基礎(chǔ)知識進(jìn)階
Mysql基礎(chǔ)知識完整版
健康管理系統(tǒng)學(xué)習(xí)花絮(學(xué)習(xí)記錄)
Node.js基礎(chǔ)知識(隨手筆記)
MongoDB基礎(chǔ)知識
Dubbo學(xué)習(xí)筆記
Vue學(xué)習(xí)筆記(隨手筆記)
聲明:發(fā)表此文是出于傳遞更多信息之目的蛾洛。若有來源標(biāo)注錯(cuò)誤或侵犯了您的合法權(quán)益养铸,請作者持權(quán)屬證明與本我們(QQ:981086665;郵箱:981086665@qq.com)聯(lián)系聯(lián)系雅潭,我們將及時(shí)更正揭厚、刪除,謝謝扶供。