The `app.get()` Function in Express

The `app.get()` Function in Express

Express' app.get() function lets you define a route handler for GET requests to a given URL. Express apps have a `get()` function that creates a new route handler for GET requests. Here's what you need to know.

Express' app.get() function lets you define a route handler for GET requests to a given URL. For example, the below code registers a route handler that Express will call when it receives an HTTP GET request to /test.

const app = require('express')();

// If you receive a GET request with `url = '/test'`, always
// send back an HTTP response with body 'ok'.
app.get('/test', function routeHandler(req, res) {
  res.send('ok');
});

Regular Expressions

The first parameter to app.get() is called the path. The path string supports several special characters that let you use a subset of regular expressions in path strings.

For example, you may see app.get('*') in tutorials: this registers a route handler for all GET requests.

const app = require('express')();

// Send back an HTTP response with body 'ok' whenever you
// receive a GET request, regardless of the URL.
app.get('*', function routeHandler(req, res) {
  res.send('ok');
});

?, +, *, (, ), and : are special characters in Express paths. Although you typically shouldn't use these characters as literals in URLs, you can escape them with a \:

const app = require('express')();

// Escape '+' so it is treated as a literal.
app.get('/\\+', function routeHandler(req, res) {
  res.send('ok');
});

We typically don't recommend using *, +, ?, (, or ) in Express route paths. They are rarely useful, especially since Express supports specifying a JavaScript regular expression as a path:

const app = require('express')();

// Handle any GET request whose URL starts with '/test/'
app.get(/^\/test\//i, function routeHandler(req, res) {
  res.send('ok');
});

express node javascript web-development developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Node.JS Developers | Skenix Infotech

We are providing robust Node.JS Development Services with expert Node.js Developers. Get affordable Node.JS Web Development services from Skenix Infotech.

Node.js Express Text Repeater Generator Online Tool in Javascript Full Tutorial 2020

Node.js Express Text Repeater Generator Online Tool in Javascript Full Tutorial 2020

Node.js Express Youtube Video URL Timestamp Link Generator in JavaScript Full Project 2020

Node.js Express Youtube Video URL Timestamp Link Generator in Javascript Full Project 2020

Node.js Express Minify JSON Online Converter Full Web App Deployed to Live Website 2020

Node.js Express Minify JSON Online Converter Full Web App Deployed to Live Website 2020

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...