Cannot GET from Express server in production, 404 (Not Found)
  • 17-May-2023
Lightrun Team
Author Lightrun Team
Share
Cannot GET from Express server in production, 404 (Not Found)

Cannot GET from Express server in production, 404 (Not Found)

Lightrun Team
Lightrun Team
17-May-2023

Explanation of the problem

 

When attempting to deploy a Nuxt app with an Express server on platforms like Heroku and Netlify, a recurring issue arises where the deployed application returns a 404 error. This error is consistent across different deployment environments, suggesting a potential misconfiguration in the Nuxt/Express setup. Even a test app created with the default scaffolding experiences the same problem.

The issue manifests itself when trying to perform a GET request to a test endpoint from the client-side code. Locally, the request works fine and logs the expected response to the console. However, when deployed to Heroku, for example, the console displays a “404 (Not Found)” error. The following code snippets showcase the client-side button click event and the server-side test endpoint:

 

<template>
  <section class="container">
    <button @click="logTest">Log Test</button>
  </section>
</template>

<script>
import axios from 'axios'

export default {
  methods: {
    logTest() {
      return axios.get('/test').then((res) => {
        console.log('got data:', res.data)
      })
    }
  }
}
</script>

 

// server/index.js
app.get('/test', (req, res) => {
  res.status(200).send('reached test endpoint')
})

 

Despite the consistent local functionality, the deployed application fails to locate the “/test” endpoint, resulting in a 404 error. This issue persists even when following deployment instructions provided by Heroku. Assistance is sought to resolve this matter, as the default scaffolding and setup remain unchanged, making it difficult to determine the cause of the error. Any help or insights into this problem would be greatly appreciated, as it has impeded progress for several days.

 

Troubleshooting with the Lightrun Developer Observability Platform

 

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for: Cannot GET from Express server in production, 404 (Not Found)

 

To resolve the issue of a 404 error when accessing the “/test” endpoint in a deployed Nuxt app with an Express server, the following steps can be taken:

  1. Check the Deployment Configuration: Verify that the deployment process is correctly set up and that all necessary dependencies and build steps are included. Double-check any environment variables or configuration files that might affect the server-side routing.
  2. Configure Server to Handle All Routes: Ensure that the server is configured to handle all routes and redirect them to the appropriate Nuxt page. This can be achieved by adding a wildcard route that redirects all requests to the Nuxt app’s entry point. Modify the server configuration code in “server/index.js” to include the following:

 

// server/index.js
const { resolve } = require('path');
const express = require('express');
const app = express();

// ...existing code...

// Add this route to handle all requests
app.get('*', (req, res) => {
  const indexPath = resolve(__dirname, '../dist/index.html');
  res.sendFile(indexPath);
});

// ...existing code...

 

  1. Build and Deploy the Application: After making the necessary changes, rebuild the Nuxt application and redeploy it to the chosen hosting platform. This typically involves running the build command (npm run build) and deploying the resulting build files to the appropriate server or hosting service.

By following these steps, the Nuxt app with the Express server should correctly handle the routes, allowing access to the “/test” endpoint without encountering a 404 error during deployment.

 

Problems with create-nuxt-app

 

Problem 1: One common problem with create-nuxt-app is the inability to install dependencies due to network restrictions or other issues. This can result in a failed installation process, preventing users from creating a new Nuxt.js application. The error message might look like this:

 

npm ERR! code ECONNREFUSED
npm ERR! errno ECONNREFUSED
npm ERR! FetchError: request to https://registry.npmjs.org/create-nuxt-app failed, reason: connect ECONNREFUSED 127.0.0.1:443

 

Solution: To overcome this issue, you can try using a different package registry or configure npm to use a proxy if you are behind a firewall. You can set the registry to npm’s default registry by running the following command:

 

npm config set registry https://registry.npmjs.org/

 

If you need to use a different registry, specify it with the --registry flag during the create-nuxt-app command. For example:

 

npx create-nuxt-app my-app --registry=https://registry.npm.taobao.org

 

Problem 2: Another common problem is encountering compatibility issues with Node.js or npm versions. This can lead to installation errors or unexpected behavior when using create-nuxt-app. For instance, you may see an error like this:

 

Error: Cannot find module '.../create-nuxt-app/bin/create-nuxt-app.js'

 

Solution: To resolve compatibility issues, make sure you are using a supported version of Node.js and npm. Refer to the Nuxt.js documentation or the create-nuxt-app GitHub repository for the recommended Node.js and npm versions. You can also try updating Node.js and npm to the latest stable versions.

Problem 3: Sometimes, users may encounter permission errors during the installation of create-nuxt-app, preventing them from creating a new Nuxt.js project. The error message could be something like:

 

EACCES: permission denied, mkdir '/usr/local/lib/node_modules/create-nuxt-app'

 

Solution: To resolve permission errors, you can either run the installation command with administrative privileges (using sudo on Unix-based systems) or configure npm to install global packages in a directory where you have write permissions. For example:

 

npm config set prefix ~/.npm-global

 

Then, make sure the ~/.npm-global/bin directory is added to your system’s PATH variable.

 

A brief introduction to create-nuxt-app

 

create-nuxt-app is a command-line tool that enables developers to quickly scaffold a Nuxt.js application with predefined configurations and project structure. It simplifies the setup process by generating the necessary files and dependencies, allowing developers to focus on building their Nuxt.js projects. With create-nuxt-app, developers can easily create a new Nuxt.js project without manually configuring the build system, server, and other project settings.

Internally, create-nuxt-app utilizes the popular package manager npm to manage project dependencies and execute the necessary installation commands. It leverages the official Nuxt.js templates to generate the initial project structure, including the directory layout, configuration files, and sample components. By default, create-nuxt-app sets up a Universal (SSR) rendering mode with optional features like Axios for HTTP requests and Jest for testing. Developers can also customize the project settings during the scaffolding process or modify them later as per their requirements.

Overall, create-nuxt-app simplifies the initial setup of a Nuxt.js application by automating the configuration process and providing a well-organized project structure. It allows developers to kickstart their Nuxt.js projects efficiently and focus on implementing the desired functionality without getting bogged down by complex configuration tasks.

 

Most popular use cases for create-nuxt-app

 

  1. Rapid Nuxt.js Application Setup: create-nuxt-app is designed to streamline the process of setting up a new Nuxt.js application. It generates a complete project structure with all the necessary configuration files and dependencies, saving developers valuable time and effort. By running a simple command, developers can quickly scaffold a Nuxt.js project and start building their application.

 

npx create-nuxt-app my-app

 

  1. Customizable Project Configuration: create-nuxt-app provides options for customizing the project configuration during the scaffolding process. Developers can choose the programming language, package manager, UI framework, server framework, rendering mode, and other relevant settings. This flexibility allows developers to tailor the project setup according to their specific requirements.

 

npx create-nuxt-app my-app
# Follow the interactive prompts to customize the project configuration

 

  1. Integration with Additional Features and Modules: create-nuxt-app integrates with various additional features and modules to enhance the functionality of a Nuxt.js application. It supports the inclusion of popular modules like Axios for HTTP requests and Jest for testing. By selecting these options during project creation, developers can seamlessly integrate these features into their Nuxt.js application, enabling efficient data fetching and robust testing capabilities.

 

npx create-nuxt-app my-app
# Select the desired modules like Axios and Jest during the project setup

 

In summary, create-nuxt-app simplifies the process of setting up a Nuxt.js application by providing a quick and customizable project scaffold. It empowers developers to rapidly start their Nuxt.js projects with pre-configured settings and optional modules, enabling them to focus on writing code and building innovative web applications.

 

Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.