Appearance
Run multiple commands
In this tutorial we will write a script that runs multiple commands in sequence.
More specifically, just with one click, our script will:
- Initialize a new NPM project
- Install the
express
NPM package (a web server library) - Write a JavaScript code for the web server
- Spin up the express server
100% automatically.
Let's try creating an example.
Create a folder named helloserver
and create a file named index.json
:
~/pinokio
/api
/helloworld
index.json
/helloserver
index.json
/bin
The index.json
should look like this:
json
{
"run": [{
"method": "shell.run",
"params": {
"message": "mkdir engine"
}
}, {
"method": "shell.run",
"params": {
"message": "npm init -y",
"path": "engine"
}
}, {
"method": "shell.run",
"params": {
"message": "npm install express",
"path": "engine"
}
}, {
"method": "fs.write",
"params": {
"path": "engine/index.js",
"text": [
"const express = require('express');",
"const app = express();",
"app.get('/', function (req, res) {",
" res.send('<h1>Hello World</h1>');",
"});",
"console.log('starting server')",
"app.listen(3000, () => { console.log ('server started') });"
],
"join": "\n"
}
}, {
"method": "shell.run",
"params": {
"message": "node index",
"path": "engine"
}
}]
}
- The first step runs
mkdir helloserver
to create a folder namedhelloserver
- The second step runs
npm init -y
to initialize an NPM project - The third step runs
npm install express
to install the express.js package, which we will use to build a super simple web server. - The fourth step uses an API called fs.write to write lines to the file named
helloserver/index.js
- The final step runs
node index
to start the express server code we just wrote through thefs.write
API.
At this point your helloserver
project folder will look something like this:
~/pinokio
/api
/helloworld
index.json
/helloserver
index.json
/engine
package.json
index.js
/node_modules
/bin
Now go to http://localhost:3000
and you will see the web server is up and displaying "Hello World".