Learn Express-js - 10 Code Examples & CST Typing Practice Test
Express.js is a fast, minimalist web framework for Node.js. It simplifies building web servers, RESTful APIs, and backend services, providing robust routing, middleware support, and HTTP utility methods.
View all 10 Express-js code examples →
Learn EXPRESS-JS with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Express-js
What is Express-js?
Express.js is a fast, minimalist web framework for Node.js. It simplifies building web servers, RESTful APIs, and backend services, providing robust routing, middleware support, and HTTP utility methods.
What are the primary use cases for Express-js?
Building RESTful APIs and web services. Creating backend for web and mobile apps. Server-side rendered web applications. Rapid prototyping of server logic. Microservice architecture backends
What are the strengths of Express-js?
Minimal and flexible framework. Large ecosystem of middleware and plugins. Lightweight and performant. Easy to integrate with frontend frameworks. Well-documented with strong community support
What are the limitations of Express-js?
No built-in ORM or database abstraction. Requires manual setup for large apps. Callback-based patterns can become messy without async/await. Limited built-in security features. Not opinionated - developer must define structure
How can I practice Express-js typing speed?
CodeSpeedTest offers 10+ real Express-js code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.