Let's create a project that uses Bolt to run a simple ExpressJS server.
Adding project files
Creating Bolt Project
Adding a Bolt Service
Running Bolt Project
Listing Bolt Services
Monitoring Service Logs
Stopping Bolt Project
Adding project files
Let's add these files in our project directory.
# Below's the directory structure of our project.
bolt-project/
╰─➤ service-one/
╰─➤ index.js
╰─➤ package.json
╰─➤ .env.tpl
// Below's our service-one/index.js file
const express =require('express');
const app =express();
const dotenv =require("dotenv")
dotenv.config()
const port = process.env.ASSIGNED_PORT||3000;
app.get('/',(req, res)=>{
res.send('Hello World!');
});
app.listen(port,()=>{
console.log(`Example app listening on port ${port}`);
});
// Below's our service-one/package.json file
{
"name":"service-one",
"version":"0.0.1",
"description":"",
"license":"ISC",
"main":"index.js",
"scripts":{
"start":"node index.js"
},
"engines":{
"node":">=18 <19"
},
"dependencies":{
"express":"^4.18.2",
"dotenv":"^16.3.1"
}
}
Creating Bolt Project
Once you have all the files and directories as mentioned above, you can now run bolt init command to create bolt.yaml file.
$ cd bolt-project
$ bolt init
This will create a bolt.yaml file in your project's root directory.
envfile: .env.tpl
project_id:'1687780932260'
project_name: boltproject
services:null
ingress:null
Adding a Bolt Service
Now, let's add our first service to the bolt.yaml file by running the following command from your project's root directory.
$ bolt service:add myapp service-one/
Creating app in service-one
Scanning source code
Detected Node.js app
Installed myapp servicein service-one
Verifying metadata for other services..
Metadata verified
Bolt service:add command runs and automatically detects the type of service you are using and creates a bolt.service.yaml template init & includes the same into your bolt.yaml file.
# This is how your bolt.yaml file looks
envfile: .env.tpl
project_id:'1687780932260'
project_name: boltproject
services:
myapp:
path: service-one/
ingress:null
# This is how your service-one/bolt.service.yaml looks
container_name: myapp
stateless:true
default_service_runner: local
service_discovery_offset:
-9000
supported_service_runners:
- local
- docker
service_runners:
local:
envfile: .env
build: npm install && node app.js
ports:
- ${ASSIGNED_PORT}:${ASSIGNED_PORT}
docker:
envfile: .env
build: ./run.Dockerfile
ports:
- ${ASSIGNED_PORT}:${ASSIGNED_PORT}
volumes:
- service-one:/app
- /app/node_modules
Running Bolt Project
Now, let's run our service using Bolt.
$ bolt up
>> Creating Ingress todo-app...
>> No ingress found in config. Skipping route generation...