Path Location for Express Router

Which location is considered the most optimal for setting URL paths in Express routing?

1) Placing the path inside the main file:

// server.js
server.use('/folder', some_router);

// some_router.js
routes.all('/', (req, res) => {
   res.status(404).end();
});

2) Including the path inside the router file:

// server.js
server.use(some_router);

// some_router.js
routes.all('/folder', (req, res) => {
   res.status(404).end();
});

Answer №1

Utilize both methods; utilize the initial technique for routing with a set sub-path as shown below:

// server.js
server.use('/api', api_router);    // Example: api/test1, api/test2, ...

Then, employ the second method for routes that are located at the root.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for passing the textbox name and value to AJAX within a PHP environment for validation purposes

Trying to send the content of a textbox via AJAX and use that information in PHP for validation purposes. Attempted to access the name of the textbox using this.name, but realized it was incorrect. Here is the AJAX code snippet: <script type="text/Java ...

Looking for assistance with $.ajax - How can I send an object with key-value pairs?

I want to utilize $.ajax to send data in this manner: $.ajax({'url': 'my.php', 'type': 'POST', 'data': arr, 'success': function(response) { alert(res ...

Discover a method to conceal an element within a <div> by monitoring mouseover events in a separate <div> container

<div id="one-id"> <div id="some">Information</div> <div id="control"> <div id="value-1"> <img id="image-one-id" /> <img id="image-two-id" /> ...

How to generate and modify a .txt file using JavaScript

I am currently working on a website that keeps track of the individuals who visit it For this purpose, I have designed an HTML form page where users can input their names However, I am facing difficulty in figuring out how to store and maintain this reco ...

Multiple requests are being sent via AJAX

Despite numerous other posts addressing the same issue, I have not been able to find a solution for my problem. The AJAX request appears to be sent multiple times. var checkAllStatus = function () { $.ajax({ cache: false, type: "POST", ...

PHP array does not retain a variable

While building a website, I encountered a perplexing bug during coding. The issue arises when I store MySQL query results in an array and then call a JavaScript function with that data. Initially, the array contains 9 items: 8 of type tinyint(4) (from the ...

Encountered an issue locating the 'express' module when executing nodemon, resulting in an error and causing the application to crash

Recently, I initiated a new project titled "project2" by running the command "create new project : express --view=ejs project2" in a directory where nodemon was already installed. However, upon entering "nodemon" in the terminal, an error message appeared ...

Creating a cutting-edge Windows Phone 8 application with JavaScript on Visual Studio 2013 Ultimate, recently updated to update 2

I am trying to create a windows phone 8 app using java-script in visual studio ultimate 2013 update 2, but I cannot seem to find the option for windows phone 8. Only the option for 8.1 is available. Can someone guide me on how to develop an app for windo ...

select attribute not saving on change

My select input has the multiple attribute being set using jQuery. Interestingly, after confirming through console.log that the code functions correctly, the multiple attribute seems to disappear when I check again. Any ideas on why this is happening? The ...

Is it possible to use Three.js to generate a single mesh that can replace multiple individual objects?

Check out my latest project at this link: maison.whiteplay.fr I'm currently working on creating a 3D PacMan game. In my code, I am using mesh to construct the level, but I'm facing performance issues due to the large number of individual bubble ...

Deleting specific arrays by selecting checkboxes and removing them in Vue.js

<script> import { datalist } from "./datalist"; export default { name: "HelloWorld", components: {}, data() { return { items: datalist, }; }, methods: { deleteEvent(id) { this.items = this.items.filter((e) => e.id ...

The tooltip being displayed is plain and lacks any design elements

When I hover over my a element, only a simple tooltip appears without any styling, unlike what is shown in the Bootstrap documentation. (I am creating the a element using JavaScript) HTML <!DOCTYPE html> <html lang="en"> <head> ...

Learn the process of extracting query parameters from a URL with the format `example.com/path#p1=v1&p2=v2...` using Angular

Callback URLs from singly.com are coming in the format of example.com/path#p1=v1&p2=v2... Is there a more Angular-friendly way to extract query parameters since $location.search and $routeParams do not work without the presence of the ? in the URL? ...

How can I change :hover to a clickable element instead?

I attempted to create a full-width accordion with the following code: .page { margin: 0; padding: 0; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; height: 100vh; } .content { -webkit- ...

Utilize model instance methods in Sails.js for enhanced functionality within views

Is there a way to retain model instance functions when passed to a view? For example, my User model has a method called fullName which combines first name, last name, and prefix. In the controller: User.find().done(function(err,users){ res.view({ ...

Having trouble displaying results in Vue.js after making an API request?

I am facing challenges in displaying the results using vue.js. The data from my API (ASP.NET CORE) is being retrieved successfully, as shown in my vue dev tools on Google Chrome. However, I am encountering difficulties in rendering the results on the brows ...

Ajax data is not successfully reaching the designated URL when posted

My task involves sending data to a PHP site that contains code to be executed when the ID #mR-RateableFramePicture is clicked on the first page. This is achieved through an AJAX request: $('#mR-RateableFramePicture').dblclick(function() { ...

For each array element that is pushed, create and return an empty object

I am encountering an issue with an array where the objects are being generated by a push operation within a function. Despite successfully viewing the objects directly in the array, when I attempt to use forEach to count how many times each id uses the ser ...

Using AJAX to Query a PHP Database

Currently, I am implementing an AJAX call from the YouTube player JavaScript to a PHP page that contains various functions, mainly involving database inserts. In my PHP code, I use a case statement to determine which function is being called based on the d ...

Having trouble importing from the src folder in Expo React

I am currently using expo-cli. When I import a module from the 'src' folder (which I created), it works perfectly fine when opened in a web browser, but encounters an error when opened on an Android device. I have tried using absolute paths and ...