ExpressJS
- Server Setup
- Routing
- Response method quick reference and examples
- Trouble shooting
- Additional Resources
Server Setup
Install Dependencies
npm install express cors dotenv
Install the nodemon
package which automatically detects code changes and restarts the server
whenever we make a code change.
npm install nodemon --save-dev
server.js
const express = require('express')
const app = express()
const PORT = process.env.PORT || 5000
require('dotenv').config(); // load environment variables
/**
* Middleware
* ================================================================
* You can add middleware here, such as body parsing for JSON data,
* authentication, error handling middleware, etc.
*/
app.use(express.json()); // Middleware to parse JSON requests
/**
* Start the server
* ================================================================
* Listen on the port specified in the .env file or default to 5000
*/
app.listen(PORT, () => {
console.log(`Server Running on Port ${PORT}`)
})
You can either start the server using the regular Node.js
command or use nodemon
for automatic
server restarts when the code changes.
node server.js
nodmon server.js
path
import * as url from 'url';
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
Routing
Routing involves defining routes on the server to handle incoming requests from clients (usually browsers or other applications) and deciding how the server should respond to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).
- Route paths, in combination with a request method, define the endpoints for a requests
- Route paths can be strings, string patterns, or regular expressions
Defining routes
Routes are defined using various HTTP methods (GET, POST, PUT, DELETE, etc.) and URL patterns. Each route corresponds to a specific endpoint on your server.
Route Parameters
Request URL: http://domain.com/users/27
app.get('/users/:id/books/:bookId', (req, res) => {
res.send(req.params)
})
Response method quick reference and examples
res.json()
Send a JSON response // http://localhost:3232/api/json-response
app.get('/api/json-response', (req, res) => {
res.json({ message: 'Hey there!' })
})
res.sendFile()
Send a HTML (file) response
Using static (TBD)
express.static(root, [options])
app.use(express.static('public'))
Using path
import path from 'path';
app.get('/my-html', (req, res) => {
const path = path.join(__dirname, 'path', 'to', 'your', 'file.html');
res.sendFile(path);
});
Method | Description |
---|---|
res.send() |
Send a response of various data types (e.g., JSON, HTML). |
res.sendStatus() |
Set the response status code and send its string version. |
res.redirect() |
Redirect to a specified URL. |
res.render() |
Render a view template with optional data. |
res.download() |
Prompt a file download with the given file path. |
res.end() |
End the response process without sending any data. |
res.setHeader() |
Set a single header value for the response. |
res.set() |
Set multiple header values for the response. |
res.get() |
Get the value of a response header. |
res.status() |
Set the HTTP status code for the response. |
res.type() |
Set the Content-Type for the response. |
res.cookie() |
Set a cookie in the response header. |
res.clearCookie() |
Clear a previously set cookie. |
res.locals |
An object for passing data to view templates. |
res.append() |
Append additional headers to the response. |
res.redirect() |
Redirect the request to another URL. |
Trouble shooting
Endpoint not working as expected
Make sure you have the correct HTTP
method (GET
, POST
, PUT
, DELETE
, etc.)
ReferenceError: __dirname is not defined
The __dirname
variable is not available in ES6 modules like it is in CommonJS modules. Instead,
you can use the import.meta.url
property to get the current module's URL and then extract the
directory path from it.