Learn EXPRESS-JS with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Express.js Simple API
const express = require('express')
const app = express()
const port = 3000
app.use(express.json())
app.get('/', (req, res) => {
res.send('Hello, Express!')
})
let count = 0
app.get('/counter', (req, res) => {
res.json({ count })
})
app.post('/counter/increment', (req, res) => {
count++
res.json({ count })
})
app.post('/counter/decrement', (req, res) => {
count--
res.json({ count })
})
app.post('/counter/reset', (req, res) => {
count = 0
res.json({ count })
})
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
})
Demonstrates a simple Express.js REST API with basic routes and middleware.
2
Express.js Query Params Example
const express = require('express')
const app = express()
const port = 3000
app.get('/greet', (req, res) => {
const name = req.query.name || 'Guest'
res.send(`Hello, ${name}!`)
})
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
})
Handle query parameters in GET requests.
3
Express.js URL Params Example
const express = require('express')
const app = express()
const port = 3000
app.get('/user/:id', (req, res) => {
const userId = req.params.id
res.send(`User ID: ${userId}`)
})
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
})
Handle dynamic URL parameters in Express routes.
4
Express.js JSON Body Example
const express = require('express')
const app = express()
const port = 3000
app.use(express.json())
app.post('/echo', (req, res) => {
res.json({ received: req.body })
})
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
})
Parse JSON body data in POST requests.
5
Express.js Middleware Example
const express = require('express')
const app = express()
const port = 3000
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`)
next()
})
app.get('/', (req, res) => {
res.send('Middleware example')
})
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
})
Demonstrates custom middleware logging requests.
6
Express.js Static Files Example
const express = require('express')
const app = express()
const port = 3000
app.use(express.static('public'))
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
})
Serve static files using Express.
7
Express.js Router Example
const express = require('express')
const app = express()
const router = express.Router()
const port = 3000
router.get('/hello', (req, res) => {
res.send('Hello from router')
})
app.use('/api', router)
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
})
Organize routes using Express Router.
8
Express.js Error Handling Example
const express = require('express')
const app = express()
const port = 3000
app.get('/error', (req, res) => {
throw new Error('Something went wrong')
})
app.use((err, req, res, next) => {
console.error(err.message)
res.status(500).send('Server Error')
})
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
})
Custom error handling middleware in Express.
9
Express.js CORS Example
const express = require('express')
const cors = require('cors')
const app = express()
const port = 3000
app.use(cors())
app.get('/', (req, res) => {
res.send('CORS enabled')
})
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
})
Enable CORS for all routes using Express.js.
10
Express.js Environment Variables Example
require('dotenv').config()
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
app.get('/', (req, res) => {
res.send(`Server running on port ${port}`)
})
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
})
Access environment variables using Express and Node.js.