DEV Community

Cover image for Create a Node App in a Minute
Khaled Md Saifullah
Khaled Md Saifullah

Posted on

Create a Node App in a Minute

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
Enter fullscreen mode Exit fullscreen mode

Tasks

  1. Create a file cmd.sh is your working directory. Make sure that the file extension should be .sh
  2. Copy the code above to the cmd.sh file then save the file
  3. Now make thew cmd.sh file executable. For that open your terminal and execute the code below
chmod +x cmd.sh
Enter fullscreen mode Exit fullscreen mode
  1. Finally execute the file using your terminal
./cmd.sh
Enter fullscreen mode Exit fullscreen mode

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.

YouTube Video

https://youtu.be/0qSe4aWmSgM

Keep Coding...✌️✌️

Top comments (0)