Skip to content

Darshan-SD/nodejs-api-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node.js API Cheatsheet

A quick reference guide for building APIs with Node.js using Express.

Table of Contents

  1. Setting Up a Basic Node.js Server
  2. Basic Routing
  3. Handling JSON and URL-Encoded Data
  4. Query Parameters and Route Parameters
  5. Handling Errors
  6. Middleware
  7. Connecting to a Database (MongoDB)
  8. Environment Variables
  9. CORS Setup
  10. Authentication with JWT

1. Setting Up a Basic Node.js Server

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

2. Basic Routing

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.post('/api/data', (req, res) => {
  res.send('Data received');
});

3. Handling JSON and URL-Encoded Data

app.use(express.json()); // To parse JSON bodies
app.use(express.urlencoded({ extended: true })); // To parse URL-encoded bodies

4. Query Parameters and Route Parameters

Query Parameters

app.get('/search', (req, res) => {
  const query = req.query.q;
  res.send(`Search query: ${query}`);
});

Route Parameters

app.get('/user/:id', (req, res) => {
  const userId = req.params.id;
  res.send(`User ID: ${userId}`);
});

5. Handling Errors

app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).send('Something went wrong!');
});

6. Middleware

const logger = (req, res, next) => {
  console.log(`${req.method} ${req.url}`);
  next();
};

app.use(logger);

7. Connecting to a Database (MongoDB)

const mongoose = require('mongoose');

mongoose.connect('mongodb://Your MongoDB URI', {
  useNewUrlParser: true,
  useUnifiedTopology: true,
}).then(() => {
  console.log('Connected to MongoDB');
}).catch(err => {
  console.error('Failed to connect to MongoDB', err);
});

8. Environment Variables

require('dotenv').config();

const dbUri = process.env.DB_URI;

9. CORS Setup

const cors = require('cors');

app.use(cors({
  origin: 'http://example.com', // Replace with your client's URL
  methods: ['GET', 'POST'],
}));

10. Authentication with JWT

const jwt = require('jsonwebtoken');

// Generate Token
const token = jwt.sign({ userId: 123 }, 'secretKey', { expiresIn: '1h' });

// Middleware to Verify Token
const verifyToken = (req, res, next) => {
  const token = req.header('Authorization').replace('Bearer ', '');
  jwt.verify(token, 'secretKey', (err, decoded) => {
    if (err) return res.status(401).send('Access Denied');
    req.user = decoded;
    next();
  });
};

app.get('/protected', verifyToken, (req, res) => {
  res.send('This is a protected route');
});

About

A guide to setup Nodejs project.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published