Build Your First Project with comNG — Step-by-Step Tutorial

Build Your First Project with comNG — Step-by-Step Tutorial

Overview

This tutorial walks you through creating a simple, functional project using comNG. Assumptions: you have a working development environment (Node.js v18+, npm or yarn), basic familiarity with the command line, and a code editor.

1. Create the project folder

  1. Open a terminal.
  2. Run:

bash

mkdir comng-first-project cd comng-first-project

2. Initialize the project

  1. Initialize npm:

bash

npm init -y
  1. Install comNG (assumes package name comng):

bash

npm install comng

3. Create the application entry

  1. Create src/index.js:

javascript

import comNG from ‘comng’; const app = comNG(); app.get(’/’, (req, res) => { res.send({ message: ‘Hello from comNG!’ }); }); const port = process.env.PORT || 3000; app.listen(port, () => console.log(</span><span class="token template-string" style="color: rgb(163, 21, 21);">comNG app listening on </span><span class="token template-string interpolation interpolation-punctuation" style="color: rgb(57, 58, 52);">${</span><span class="token template-string interpolation">port</span><span class="token template-string interpolation interpolation-punctuation" style="color: rgb(57, 58, 52);">}</span><span class="token template-string template-punctuation" style="color: rgb(163, 21, 21);">));

4. Add start script

  1. Update package.json “scripts”:

json

“scripts”: { “start”: “node –experimental-modules src/index.js” }

5. Run the app

  1. Start:

bash

npm start
  1. Visit http://localhost:3000 and confirm JSON response:

json

{ “message”: “Hello from comNG!” }

6. Add a simple route and data store

  1. Create src/data.js:

javascript

export const items = [];
  1. Update src/index.js:

javascript

import comNG from ‘comng’; import { items } from ’./data.js’; const app = comNG(); app.use(comNG.json()); app.get(’/items’, (req, res) => res.send(items)); app.post(’/items’, (req, res) => { const { name } = req.body; if (!name) return res.status(400).send({ error: ‘Name required’ }); const item = { id: items.length + 1, name }; items.push(item); res.status(201).send(item); });

7. Test endpoints

  1. Using curl:

bash

curl http://localhost:3000/items curl -X POST http://localhost:3000/items -H “Content-Type: application/json” -d ’{“name”:“Sample”}’

8. Basic error handling and validation

Add to src/index.js:

javascript

app.use((err, req, res, next) => { console.error(err); res.status(500).send({ error: ‘Internal Server Error’ }); });

9. Prepare for deployment

  1. Add a production start script if needed:

json

“start:prod”: “node src/index.js”
  1. Commit files and deploy to your preferred host (e.g., Vercel, Heroku, DigitalOcean).

10. Next steps (suggested)

  • Add persistent storage (Postgres, MongoDB).
  • Add authentication (JWT).
  • Write tests (Jest).
  • Add CI/CD pipeline.

If you want, I can generate the full project file tree and exact package.json for you.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *