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
- Open a terminal.
- Run:
bash
mkdir comng-first-project cd comng-first-project
2. Initialize the project
- Initialize npm:
bash
npm init -y
- Install comNG (assumes package name comng):
bash
npm install comng
3. Create the application entry
- 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
- Update package.json “scripts”:
json
“scripts”: { “start”: “node –experimental-modules src/index.js” }
5. Run the app
- Start:
bash
npm start
- Visit http://localhost:3000 and confirm JSON response:
json
{ “message”: “Hello from comNG!” }
6. Add a simple route and data store
- Create src/data.js:
javascript
export const items = [];
- 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
- 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
- Add a production start script if needed:
json
“start:prod”: “node src/index.js”
- 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.
Leave a Reply