标签:connect 启动失败 use ati express node.js god 成功 数据库连接
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
}));
标签:connect 启动失败 use ati express node.js god 成功 数据库连接
原文地址:https://www.cnblogs.com/gudun/p/9170356.html