child_process模块是nodejs的一个子进程模块,能够用来建立一个子进程,并执行一些任务。执行一些什么任务呢?shell命令知道吧,有了child_process模块,就能够直接在js里面调用shell命令去完成一些很是酷炫的操做了!!
举个栗子,GitHub、码云等git代码托管网站,都会有个webHook功能,当push了新的代码后,服务器能够开辟一个接口去接受这个webHook的请求,并进行git pull
、npm run build
等命令,从而达到自动化部署的目的!html
前端直接简单用的vue-cli
脚手架新建了个项目,后端是用的express前端
前端代码就不晒了,都是脚手架生成的,后端代码主要就是一个server.js
和一个执行shell的方法。vue
backend/server.js
注意先要安装几个依赖:express
和body-parser
express
是主角,不用多说,body-parser
是用来解析post请求的参数的。
const express = require('express'); const app = express(); const port = process.env.PORT || 8080; const www = process.env.WWW || './fontend/dist'; var bodyParser = require('body-parser')//格式化body数据 app.use(bodyParser.urlencoded({extended: false}));//body parser插件配置 app.use(bodyParser.json());//body parser插件配置 const gitPush = require('./service/git-push')//引入写的服务 app.post('/api/git_hook',async (req, res) => {//监听这个接口 if(req.body.password !== '666'){// 这里校验post请求的密码 res.send('密码错误') return } const code = await gitPush() res.send('hello world' + code) }) app.use(express.static(www)); console.log(`serving ${www}`); app.get('*', (req, res) => { res.sendFile(`index.html`, { root: www }); }); app.listen(port, () => console.log(`listening on http://localhost:${port}`));
const childProcess = require('child_process'); const path = require('path') module.exports = async function (params) { await createGitPullPromise() return await createPackPromise() } function createPackPromise(){ return new Promise((res, rej) => { const compile = childProcess.spawn('npm', ['run', 'build'], {cwd: path.resolve(__dirname, '../../fontend')}) compile.on('close', code => { // console.log(code) res(code) }) }) } function createGitPullPromise(){ return new Promise((res, rej) => { const compile = childProcess.spawn('git', ['pull'], {cwd: path.resolve(__dirname, '../../fontend')}) compile.on('close', code => { // console.log(code) res(code) }) }) }
child_process模块,主要是用的child_process.spawn(),须要注意的是,这个函数只会建立异步进程,具体的API能够参考官网。异步进程的话,不会阻塞主进程的执行,因此我backend/service/git-push.js
里面用async function
来进行异步回调的控制。child_process模块还提供了建立同步子进程的方法 child_process.spawnSync,了解nodejs比较多的同窗可能会发现,跟异步的方法相比,就是最后面加了个Sync
,嗯,也能够这么理解吧。
但愿你们多了解下这个模块,多动手操做下,能用到哪里 留下了很是大的想象空间!node