zoukankan      html  css  js  c++  java
  • 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
    }));

  • 相关阅读:
    (转)Apache与Tomcat 区别联系
    (转)JAVA排序汇总
    (转)Java线程:大总结
    (转)Java线程:新特征-原子量,障碍器
    (转)Java线程:新特征-条件变量
    oracle中的not in和not exists注意事项
    oracle字符乱码的解决方法
    线刷和卡刷的区别
    nexus5刷机
    linux上复制行到另一个文件
  • 原文地址:https://www.cnblogs.com/gudun/p/9170356.html
Copyright © 2011-2022 走看看