Back to home

搭建部署Mock-Server

获取最小的nodejs容器镜像(几十M)

docker pull smebberson/alpine-nodejs

启动镜像实例

~#cd /mnt/disk2/
~#git config --system http.sslverify false
~#docker run --name=mock-server -d \
--volume=/mnt/disk2/mock-server:/root/mock-server \
--publish 9999:9999 smebberson/alpine-nodejs:latest

编译mock-server

~#docker-enter mock-server
mock-server
data_template data -r
npm install && npm run build

方便对data/*.json做版本控制

vi server.js
app.use('/~m/app', require('./routes/app')); // management app route^M
app.use('/~m/list', require('./routes/list')); //添加这行
app.use('/~m/token', require('./routes/token')); // management token route^M
vi routes/list.js
var url = require('url');
var path = require('path');
var router = require('express').Router();
var fs = require('fs'),
fileList = [];

function walk(path){
var dirList = fs.readdirSync(path);
dirList.forEach(function(item){
if(fs.statSync(path + '/' + item).isDirectory()){
walk(path + '/' + item);
}else{
fileList.push(path + '/' + item);
}
});
}
function list_files(){
return fileList;
}
const pathName='data';
walk(pathName);
router.route('/')

// get settings
.get((req, res) => {
let content = `

Index of ${pathName}

`;
fileList.forEach(file => {
let itemLink = path.join('',file);
const stat = fs.statSync(path.join('', file));
if (stat && stat.isDirectory()) {
itemLink = path.join(itemLink, '/');
}
content += `

${file}

`;
});
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end(content);
});

module.exports = router;

启动

nohup node server.js 9999 &