const http = require('http') , https = require('https');const express = require('express');const bodyParser = require('body-parser')const app = express();const fs = require('fs');const ejs = require('ejs');const path = require('path');const MongoClient = require('mongodb').MongoClient;// 返回信息const questions = { code: 200, msg: 'success',};// https证书,开https服务的自验证证书const options = { key: fs.readFileSync('./privatekey.pem'), cert: fs.readFileSync('./certificate.pem')};let xltitle = '标题(初始化数据)', xlcontent = '内容(初始化数据)', xlfaceid = '1(初始化数据)';const url = 'mongodb://127.0.0.1/xlbase';// 默认端口27017,无需填写// 也可以修改端口,vim /etc/mongodb.conf// 初始化数据库MongoClient.connect(url, function (err, db) { if (err) throw err; let database = db.db('xlbase'); console.log('------------数据库初始化成功------------');// 如果没有face这个集合,会创建一个,所以可以用这个来初始化集合 database.createCollection('face', function (err, res) { if (err) throw err; console.log('------------集合初始化完毕------------'); db.close(); });});//设置跨域访问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();});// parse application/x-www-form-urlencoded 解析app.use(bodyParser.urlencoded({extended: false}));// parse application/json 解析app.use(bodyParser.json());// view engine setup,视图模版app.set('views', path.join(__dirname, './'));app.set('view engine', 'jade');// 静态资源解析路径(css,js,图片等)app.use(express.static(path.join(__dirname, './static')));// 数据接收接口app.post('/info', function (req, res) { res.header("Content-Type", "application/json;charset=utf-8"); res.status(200); xltitle = req.body.title; xlcontent = req.body.content; xlfaceid = req.body.faceId; let info = { 'faceid': xlfaceid, 'title': xltitle, 'content': xlcontent }; let faceid = { 'faceid': xlfaceid }; let updateInfo = {$set: info};// 组装更新的信息 MongoClient.connect(url, function (err, db) { let database = db.db('xlbase'); database.collection('face').find(faceid).toArray(function (err, result) { if (err) throw err; // 判断集合中faceid和当前传过来的faceid是否相同和存在 // 如果不存在就新插入这条数据 // 如果存在且相同,就更新数据 if (result.length !== 0 && result[0].faceid === xlfaceid) { database.collection("face").updateOne(faceid, updateInfo, function (err, res) { if (err) throw err; console.log("------------数据更新成功------------"); db.close(); }); } else { database.collection('face').insertOne(info, function (err, res) { if (err) throw err; console.log("------------数据添加成功------------"); db.close(); }) } }) }); res.json(questions); // 返回信息 res.end(JSON.stringify(req.body, null, 2))});app.get('/index', function (req, res) { res.status(200); res.header("Content-Type", "text/html;charset=utf-8");// 根据faceId查询数据 MongoClient.connect(url, function (err, db) { if (err) throw err; let dbo = db.db("xlbase"); let face = {"faceid": xlfaceid}; // 查询条件 let xltitle1 = 404; let xlcontent1 = '网页出错!'; dbo.collection("face").find(face).toArray(function (err, result) { if (err) throw err; console.log(result); xltitle1 = result[0].title; xlcontent1 = result[0].content; db.close(); console.log('------------查询完毕------------'); res.send('<h3 style="font-size: 35px">' + xltitle1 + '</h3>' + '<pre style="white-space: pre-wrap;word-wrap: break-word;font-size: 30px">' + xlcontent1 + '</pre>'); res.end(); }); });})// 配置服务端口// http.createServer(app).listen(3001, function () {// console.log('3001')// });process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; // 绕过证书验证https.createServer(options, app).listen(8009, function () { console.log('port: 8009');});// var server = app.listen(3001, function () {//// var host = server.address().address;//// var port = server.address().port;//// console.log('Example app listening at http://%s:%s', host, port);// })