Create a Node App in a Minute

RMAG news

Starting a Node application requires creating many files and directories, which can be tiring to do for every new project. This blog offers a solution to help you repeat these tasks easily.

We can execute a bash file whenever we need to create a new node application.

Here is the bash file

#!/bin/bash
npm init -y

echo ‘{
“name”: “node”,
“version”: “1.0.0”,
“main”: “server.js”,
“scripts”: {
“start”: “node server.js”,
“dev”: “node –watch server.js”
},
“keywords”: [],
“author”: “”,
“license”: “ISC”,
“description”: “”
}’
> package.json

npm i express

touch .env .env.sample
echo “node_modules
.env”
> .gitignore

echo ‘{
“tabWidth”: 2,
“useTabs”: false,
“semi”: false,
“singleQuote”: true,
“trailingComma”: “es5”,
“bracketSpacing”: true
}’
> .prettierrc

echo ‘/.vscode
/node modules
•/dist
*.env
.env
.env.* ‘
> .prettierignore

mkdir src

cd ./src
mkdir controllers middleware db routes utils
touch app.js server.js

echo “import app from ‘./app.js’
const port = 8000
const hostname = ‘127.0.0.1’
app.listen(port, hostname, () => {
console.log(‘Server is running on port:8000’)
})”
> server.js

echo “import express from ‘express’
const app = express()
// middlewares
app.use(express.json())
export default app”
> app.js

cd ../
rm cmd.sh

Tasks

Create a file cmd.sh is your working directory. Make sure that the file extension should be .sh

Copy the code above to the cmd.sh file then save the file
Now make thew cmd.sh file executable. For that open your terminal and execute the code below

chmod +x cmd.sh

Finally execute the file using your terminal

./cmd.sh

Note: Here, If you are a Linux/Mac user the code will work perfectly. In Windows you need to use powershell/gitbash to run the bash/sh file or maybe you need to disable the MS Defender.

Keep Coding…✌️✌️