SyntaxStudy
Sign Up
Express.js Introduction to Express.js
Express.js Beginner 8 min read

Introduction to Express.js

Express.js is a minimal, fast, and unopinionated web framework for Node.js. It provides a thin layer of fundamental web application features without obscuring Node.js features. Express is the most popular Node.js web framework and forms the foundation of many other frameworks.

Example
// Install: npm install express

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

// Middleware to parse JSON bodies
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

// Basic route
app.get('/', (req, res) => {
  res.json({ message: 'Hello, Express!' })
})

// Route with parameter
app.get('/users/:id', (req, res) => {
  const { id } = req.params
  res.json({ userId: id })
})

// POST route
app.post('/users', (req, res) => {
  const { name, email } = req.body
  // Save to database...
  res.status(201).json({ id: Date.now(), name, email })
})

// Start server
app.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}`)
})