码迷,mamicode.com
首页 > Web开发 > 详细

node.js

时间:2018-06-11 23:35:18      阅读:279      评论:0      收藏:0      [点我收藏+]

标签: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
}));

node.js

标签:connect   启动失败   use   ati   express   node.js   god   成功   数据库连接   

原文地址:https://www.cnblogs.com/gudun/p/9170356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!