jackieli123723 / jackieli123723.github.io

✅lilidong 个人博客
9 stars 0 forks source link

express (vue+react)静态资源dist目录部署服务 #17

Open jackieli123723 opened 6 years ago

jackieli123723 commented 6 years ago

第一种方式

const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();

app.use(express.static(path.join(__dirname, '../dist')));

//quick and dirty react server-side routing
app.get('/', (req, res)=>{
    console.log(res);
    res.send(fs.readFileSync(path.join(__dirname, '../dist/index.html'),'utf8'));
})

app.get('/home',(req,res)=>{
    res.send(fs.readFileSync(path.join(__dirname, '../dist/index.html'),'utf8'));
})

app.get('/contact',(req,res)=>{
    res.send(fs.readFileSync(path.join(__dirname, '../dist/index.html'),'utf8'));
})

app.get('/contact/email',(req,res)=>{
    res.send(fs.readFileSync(path.join(__dirname, '../dist/index.html'),'utf8'));
})

app.get('/contact/text',(req,res)=>{
    res.send(fs.readFileSync(path.join(__dirname, '../dist/index.html'),'utf8'));
})

app.get('/microblog',(req,res)=>{
    res.send(fs.readFileSync(path.join(__dirname, '../dist/index.html'),'utf8'));
})

app.get('/education',(req,res)=>{
    res.send(fs.readFileSync(path.join(__dirname, '../dist/index.html'),'utf8'));
})

app.get('/projects',(req,res)=>{
    res.send(fs.readFileSync(path.join(__dirname, '../dist/index.html'),'utf8'));
})

app.listen(5000, ()=>{
    console.log('listening');
})

第二种方式

var http = require("http");
var fs = require("fs");
var path = require("path");
var mime = require("mime");

function send404(response) {
  response.writeHead(404, {"Content-type" : "text/plain"});
  response.write("Error 404: resource not found");
  response.end();
}

function sendFile(response, filePath, fileContents) {
  response.writeHead(200, {"Content-type" : mime.lookup(path.basename(filePath))});
  response.end(fileContents);
}

function serverWorking(response, absPath) {
  fs.exists(absPath, function(exists) {
    if (exists) {
      fs.readFile(absPath, function(err, data) {
        if (err) {
          send404(response)
        } else {
          sendFile(response, absPath, data);
        }
      });
    } else {
      send404(response);
    }
  });
}

var server = http.createServer(function(request, response) {
  var filePath = false;

  if (request.url == '/') {
    filePath = "dist/index.html";
  } else {
    filePath = "dist" + request.url;
  }

  var absPath = "./" + filePath;
  serverWorking(response, absPath);
});

var port_number = server.listen(process.env.PORT || 5000);

 console.log("代码部署成功访问端口http://localhost:5000");

第三种方式

var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');

// 相当于通过本地node服务代理请求到了http://cnodejs.org/api
var proxy = [{
    path: '/api/*',
    target: 'http://cnode.lilidong.cn',
    host: 'cnodejs.org'
}];

//启动服务
var server = new WebpackDevServer(webpack(config), {
    publicPath: config.output.publicPath,
    proxy:proxy
});

//将其他路由,全部返回index.html
server.app.get('*', function (req,res) {
    res.sendFile(__dirname + '/index.html')
});

server.listen(4040);