node.js

Posted 孤魂1715

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node.js相关的知识,希望对你有一定的参考价值。

 

const express = require("express");
const mongoose = require("mongoose");
const swig = require("swig");
const Cookies = require(‘cookies‘);

const htmlRouter = require("./routers/client/html");
const apiRouter = require("./routers/client/api");

new Promise((resolve,reject)=>{
mongoose.connect(‘mongodb://localhost:27017‘,(error)=>{
if(error){
console.log("数据库连接失败");
reject();
}else{
console.log("数据库连接成功")
resolve();
}
})
})
.then(
()=>{
const server = express();


server.use("/static",express.static(__dirname+"/static"));

server.use((request, response, next)=>{
// 创建cookie对象
let cookies = new Cookies(request, response);
response.cookies = cookies;
if(request.url!="/favicon.ico"){
next();
}
})

server.use("/api",apiRouter);

server.engine(‘html‘, swig.renderFile);
server.set(‘views‘, __dirname+‘/html/client‘);
server.set(‘view engine‘, ‘html‘);
swig.setDefaults({cache: false});


server.use("/",htmlRouter);

server.listen(8080,"localhost",(error)=>{
if(error){
console.log("服务器启动失败")
}else{
console.log("服务器启动成功")
}
})
}
)

 

//代理

const httpProxyMiddleware = require(‘http-proxy-middleware‘);

server.use(‘/restapi‘, httpProxyMiddleware({
target: ‘https://h5.ele.me‘,
changeOrigin: true
}));





















































以上是关于node.js的主要内容,如果未能解决你的问题,请参考以下文章

Node.js 入门

Node.js

Node.js

一初始Node.js

一初始Node.js

一初始Node.js