Express.js API Flashcards

(10 cards)

1
Q

What is Express and why use it?

A

Express is a framework that wraps Node’s built-in http module and removes the boilerplate. Instead of manually checking req.url and req.method for every route, Express gives you clean, readable syntax for building servers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you set up a basic Express server?

A

const express = require(‘express’)
const app = express()

app.listen(3000, () => console.log(‘Running on port 3000’))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you create routes in Express?

A

Use app.get(), app.post(), etc. Each takes a path and a callback with req and res:

app.get(‘/users’, (req, res) => {
res.json({ users: [] })
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are route parameters?

A

Dynamic values in a URL path, prefixed with :. Access them via req.params:

app.get(‘/users/:id’, (req, res) => {
console.log(req.params.id) // e.g. “42”
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is middleware?

A

A function that runs between the request coming in and the response going out. It has access to req, res, and next(). Call next() to pass control to the next middleware.

app.use((req, res, next) => {
console.log(${req.method} ${req.url})
next()
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you handle POST requests and read the body in Express?

A

Add the built-in JSON middleware first, then access req.body:

app.use(express.json()) // must add this!

app.post(‘/users’, (req, res) => {
console.log(req.body) // { name: ‘John’ }
res.json({ success: true })
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is MVC?

A

Model-View-Controller — a way to organise your code into three clear roles:

Model — handles data logic

View — what the user sees

Controller — connects the two, handles requests

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are Express Routers?

A

A way to split your routes into separate files so app.js doesn’t get massive:

// routes/users.js
const router = express.Router()
router.get(‘/’, (req, res) => res.json([]))
module.exports = router

// app.js
const usersRouter = require(‘./routes/users’)
app.use(‘/users’, usersRouter)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a RESTful API?

A

An API that follows a standard pattern using HTTP methods to perform actions on resources:

GET /launches — read all

POST /launches — create one

PUT /launches/:id — update one

DELETE /launches/:id — delete one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is CRUD?

A

The four basic operations every data-driven app needs:

Create → POST

Read → GET

Update → PUT / PATCH

Delete → DELETE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly