Implementing html5mode in Express.js and Angular.js for cleaner URLs

I've been working on resolving the issue of avoiding # in my Angular app with an ExpressJS server-side setup. I found a solution to enable html5mode and it worked well. However, whenever there is another 'get' request to fetch data from a different URL like /api/services, it seems to be broken and doesn't retrieve the data properly for the page.

Below is what I have implemented on the Express end:

router.get('/*', function (req, res, next) {
  res.render('index');
});
router.get('/api/service-edit', function (req, res, next) {
  Service.find(function (err, services) {
    if (err) {return next(err);}
    res.json(services);
  });
});

I'm not completely certain, but I suspect that the '/*' might be causing issues for the server when reading api URLs. I'm curious if any of you have encountered this problem and have a solution to troubleshoot it.

Thank you for your help.

Answer №1

When working with expressjs, it's important to note that the routing rules are matched in the order they appear in the code. This means that the '/*' rule will always be matched first, causing the second rule to never be reached.

One way to potentially solve this issue would be to reorder the rules so that the second one comes before the first.

I hope this information proves helpful for you!

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

What is it about that that ticks off so many different boxes?

Hey there! I've been working on creating checkboxes within next js that iterate through each filter and its options. Fortunately, I managed to get it up and running smoothly. However, I'm encountering an issue where checking one option in a speci ...

In Javascript, where are declared classes stored?

When working in a browser environment like Firefox 60+, I've encountered an issue while attempting to retrieve a class from the global window object: class c{}; console.log(window.c); // undefined This is peculiar, as for any other declaration, it w ...

Oops! Next.js Scripts encountered an error: Module '../../webpack-runtime.js' cannot be located

Looking to develop an RSS script with Next.js. To achieve this, I created a script in a subfolder within the root directory called scripts/ and named it build-rss.js next.config.js module.exports = { webpack: (config, options) => { config.m ...

The ACL system in Node.js that is based on resources

I am in the process of setting up a basic Access Control system in Node, and I am seeking advice on the most effective approach for what I want to achieve. Currently, I am utilizing Node ACL, but it's not entirely clear how to block access based on s ...

Error in node: nodenv could not locate any specified version for use

Following a successful installation of Node, I encountered the following error message: $ npm nodenv: couldn't find any version specified for use $ node nodenv: couldn't find any version specified for use Does anyone have insight into where thi ...

Having trouble getting Calendly Webhooks to function in a node.js environment with ngrok?

Hello everyone, this is my first time seeking help on Stack Overflow so please bear with me if there are any flaws in my question. I recently started using the Calendly Teams version and decided to implement the Webhooks feature on a Node.js web applicati ...

The setTimeout functionality is executing faster than expected

In my selenium test, I've noticed that the setTimeout function consistently finishes about 25% faster than it should. For example, when waiting for 20 seconds, the function completes after only 15 seconds. test.describe('basic login test',f ...

Error not caught: [$injector:modulerr] Visit http://errors.angularjs.org/1.7.9/$injector/moduler for more information

I encountered an error in the console while loading my application. I am new to angular and any help would be greatly appreciated. I am attempting to export the datatable into excel, and I believe the issue lies with this line: angular.module('myApp& ...

The Angular routes are not displaying correctly within the ng-view

I've been searching for a solution for my first post but can't seem to find one. The content from routing is not displaying in ng-view. index.html <html lang="en" ng-app="myApp"> <head> <script src="//ajax.googleapis.com/ajax/ ...

Choosing the following choice using the Material-UI Select tool

Looking for a way to enhance my dropdown select from MUI in a Nextjs application by adding two arrows for navigating to the next/previous option. Here's what my code currently looks like: <StyledFormControl> <Select value={cu ...

Experiencing difficulties while attempting to utilize Appium on Ubuntu, encountering a node.js:134 error

I installed nodejs by using the following command: sudo apt-get install -y nodejs Similarly, I installed appium with the command below: sudo npm install -g appium Despite the warning on the appium page advising against using sudo, I had to use it becau ...

MUI: reveal multiple selection when hovering & prevent dropdown from moving around

Utilizing material ui, I am facing two issues with a multiple select component that I cannot seem to resolve: While selecting an item, the dropdown moves around (I have already attempted solutions like MenuProps={{ variant: "menu", getContentAnc ...

What is causing the error message "TypeError: expressJwt is not a function" to appear? Is there a way to resolve it and fix the issue?

Authentication with JWT in Node.js: const expressJwt = require('express-jwt') function setupAuth() { const secret = process.env.SECRET_KEY return expressJwt({ secret, algorithms: ['HS256'] }) } module.expor ...

Launching a Phonegap app using Node.js and Express framework

My goal is to transform my current webapp into a mobile app. The existing setup consists of a nodejs-express REST API server with an HTML/JS client. I aim to utilize the API from the REST server and replace the client with a phonegap/cordova based mobile ...

What are the steps to send AJAX data before closing the page?

Trying for over 7 hours to send a value to the database when the user closes the page, like online and offline. Still struggling to find a working solution. <script tysssspe="text/javascript"> //ST window.onbeforeunload = function(){ var user_st = ...

Tips for changing the size and color of SVG images in a NextJS application

Looking to customize the color and size of an svg image named "headset.svg". Prior to this, I used next/image: <Image src={'/headset.svg'} alt='logo' width={30} height={30} className='object-contain' /> The s ...

What are the best ways to customize exported and slotted components in Svelte?

Is there a similarity between Svelte slots and vanilla-js/dom functionality (I'm having trouble with it). In html/js, I can achieve the following: <style> body {color: red;} /* style exposed part from outside */ my-element::par ...

Searching in real time using Ionic 1.x

I'm currently working on implementing a real-time search feature for my extensive product database. I attempted to replicate the provided example, and while my web service is functioning properly, I am unable to view any selectable items. Seeking gui ...

Docker: Containers experiencing stalled HTTP requests when connecting with others

For testing my web apps online, I utilize a VPS. To manage multiple web apps on the same server, I rely on Docker. Below is my docker-compose.yml version: "3.7" services: gateway: build: context: ./gateway dockerfile: Dockerfile r ...

When attempting to transfer data from a client to a server, a "Bad Request" error is encountered with the message: `POST http://localhost:5000/api

Hey everyone, I'm facing an issue with sending user data from the client-side to the server-side. Whenever I try to send new user details to the server, I encounter the following error: An alert pops up with the message "Enter valid credentials" and ...