With Express.js we can use requests and responses. Request is coming to Express and response is going from Express.
With Express we can easly send responses. With this code we’re sending string as a response:
res.send('Hi')
If we want to send json response:
res.json({
message: 'Hi',
name: 'Berkay'
})
We can change response types as well:
res.type('html') // text/html
res.type('json') // application/json
res.type('png') // image/png
Changing headers are easy:
res.set('Authorization', 'token')
res.set('Content-Type', 'application/json')
Sometimes we’ll want to add cookies to our responses:
res.cookie('token', 'supersecrettoken')
You can also delete cookies
res.clearCookie('token')
We can set our response status:
res.status(404).json({
message: 'Not found'
})
You can do the samething with a shortcut:
res.sendStatus(404) // => res.status(404).send('Not found')
With a simple response you can redirect your users:
res.redirect('https://berkaycubuk.com')
res.redirect('./')
res.redirect('back') // sending users back
Routes are simply directions where we are accepting requests. For example:
app.get('/login', (req, res) => {})
In this example, we opened our gates for the “/login” address, but only for get request.
We want to create a blog and we have “/post” route. Our visitors will be normal people so they are not using any API tools, they will use their browser to access our website. What if they only want to see a single post? We have to get some data with the url, to do that:
app.get('/post/:postName', (req, res) => {
res.send(req.params.postName)
})
This example sends user back the parameter.
Middlewares are a function that we can connect to routes. We can run it in all requests or we can run it on specific requests. They all can work together, that means you can use multiple middlewares. Let’s understand it with an example:
const express = require('express')
const app = express()
app.use(express.json())
In this example we used Express’s built in json function. It helps us to use json in our responses and requests. It runs in every request. Time to create our own and adding it to specific routes.
const myMiddleFunc = (req, res, next) => {
// do what you want in here
next() // sending the ball to the next one
}
app.get('/', myMiddleFunc, (req, res) => {})
With this, we created simple function that does what you want and when it’s finish it’s job, going to next middleware. To add middlewares to specific routes, we can pass them to route as a parameter.
I'm writing a book to simplify Hugo and allow you to create your own theme just from scratch.
Join the waitlistBook a call with me and maybe we can combine our forces to make your project a reality.