Launching the server with a custom path in Nuxt.js: Step-by-step guide

My Nuxt.js application has several nested routes.

.
├── index
│   ├── _choice
│   │   ├── city
│   │   │   ├── index.vue
│   │   │   ├── _zipCode
│   │   │   │   ├── index.vue
│   │   │   │   ├── street
│   │   │   │   │   ├── index.vue
│   │   │   │   │   └── _street.vue
│   │   │   │   └── street.vue
│   │   │   └── _zipCode.vue
│   │   ├── city.vue
│   │   ├── city.vue~
│   │   └── index.vue
│   ├── _choice.vue
│   └── index.vue
├── index.vue
└── index.vue~

When I start the server (yarn run dev), I want it to directly navigate to http://localhost:3000/1 instead of http://localhost:3000/. How can I achieve this?

Please note that in this scenario, "1" corresponds to the path "/:choice"

Answer №1

If you're in need of a solution, have you thought about implementing a middleware file to handle user redirection? I personally use one for authentication purposes - it redirects users to "/login" if they are not logged in and try to access "/admin". You could modify this approach to redirect all requests to "/".

To set up the middleware, simply create a file in the middleware folder (let's name it redirect.js) and include the following code:

export default function ({store, redirect, route}) {
    const needsRedirect = /^\/(\/|$)/.test(route.fullPath)
    if (needsRedirect) {
        return redirect('/1')
    }
    return Promise.resolve
}

Next, ensure that the file is being read by specifying it in nuxt.config.js:

router: {
    middleware: ['redirect']
},

After setting this up, all incoming requests will be redirected to "/1".

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 the best approach for setting up an Azure Web App to host both an Express static site and API

Setting up an Express app was a breeze for me, but when it comes to deploying it on Azure Web App, I'm hitting some roadblocks! The structure of my app is quite simple: a static web app with its own API. Requests to /website.com/api are forwarded to ...

Preventing Button Click with JQuery Tooltip

I've implemented a JQuery tooltip plugin on my website and it's working great. However, I'm facing an issue where I cannot click on the input button that appears when hovering over the tooltip. It seems like the button is not truly part of t ...

Input a new function

Trying to properly type this incoming function prop in a React Hook Component. Currently, I have just used any which is not ideal as I am still learning TypeScript: const FeaturedCompanies = (findFeaturedCompanies: any) => { ... } This is the plain fun ...

Unable to set a value for the variable

const readline = require('readline'); let favoriteFood; const rl = readline.createInterface(process.stdin, process.stdout); rl.question('What is your favorite food?', function(answer) { console.log('Oh, so your favorite food is &a ...

Despite population, MongooseJS still renders blank array

Currently in the process of developing an application using Node.js with MongooseJS as the middleware for handling database operations. Encountering an issue with nested schemas, specifically with one of them being populated incorrectly. Despite tracking ...

Development versions of npm libraries

Lately, I came across a library called react-3d-components that offers some d3 react components with basic charts. It's an impressive collection of components. However, when trying to access the source code due to incomplete documentation, I found my ...

Adjust the contents of an HTTP POST request body (post parameter) upon activation of the specified POST request

Is there a way to intercept and modify an HTTP Post Request using jQuery or JavaScript before sending it? If so, how can this be achieved? Thank you. ...

A guide on arranging map entries based on their values

The map displayed below, as represented in the code section, needs to be sorted in ascending order based on its values. I would like to achieve an end result where the map is sorted as depicted in the last section. If you have any suggestions or methods o ...

Convert a string to HTML using AngularJs

Snippet: <tr ng-repeat="x in orderCsList"> <td class="ctn"><input type="checkbox" ng-model="x.checked"></td> <td class="ctn">{{ x.wdate }}</td> <td class="text-left">{{ x.wid }}</td> <td class="te ...

Discover all related matching documents within a string array

I am using a mongoose schema model with a field called tags which is an array of strings to store tags for each document. I need functionality where if I search for a specific tag, such as "test," it will return all documents with tags like "testimonials" ...

I'm struggling to concentrate and unable to type in the email field

Today while working on a website, I encountered something strange. In the footer section of the site, there is a quick contact form. However, I noticed that in Firefox, I am unable to focus on the email field. Surprisingly, this issue does not occur in Chr ...

What could be causing my canvas element to only display a blank black screen?

Setting up a 3d asset viewer in Three.js can be quite challenging, especially for someone who is new to JavaScript like myself. After following the advice to wrap my code within an 'init();' function, I encountered a new issue - a black screen in ...

Navigating through tabs in a Meteor application: How to maintain the active tab when using the back button

I am working on a multi-page meteor application where each page includes a navigation template. To switch between pages, I am using iron-router. The user's current page is indicated by setting the appropriate navigation link's class to 'ac ...

The extent of the modal window (AngularJS directive)

I've been experimenting with a modal window feature using Angular UI Bootstrap. Within the parent controller, I've defined the following function: $scope.open = function () { var modalInstance = $modal.open({ templateUr ...

When attempting to reference from a variable, you may encounter an error stating that setAttribute

In my VueJS project, I am facing an issue with dynamically adding the width attribute to an inline SVG code stored in a variable called icon. Despite having the correct SVG icon code in the variable, the setAttribute method is not working as expected and t ...

Dealing with ETIMEDOUT error in Express.js

My Node.js/Express.js application interfaces with an FTP server, but crashes when the server is offline due to handling issues with the ETIMEDOUT exception. I am using the jsftp module for FTP. Here's the problematic part of my code: router.post("/" ...

Is there a way to attach a hidden input to the file input once the jquery simpleUpload function is successful?

Attempting to add a hidden form field after the file input used for uploading a file through the simpleUpload call. Here is the HTML (loaded dynamically): <div class="col-md-6"> <div class="form-group"> ...

changing four variables with a single click using Vue

I am facing a situation where I have 4 buttons that are supposed to load different components. However, every time a new component is loaded, the previous one should disappear. Here is the code snippet I currently have: const showFoo = ref(false) con ...

Is there an equivalent function to onAdd in Material-UI when using MUI Chip?

In my exploration of the latest version of Material-UI, MUI, I observed that the "onAdd" property has been removed. The only function properties remaining are "onDelete" and "onClick". I am interested in generating new chips based on user-input tags. Is ...

JavaScript's Functions and Objects: An Overview

Create a function called 'transformFirstAndLast' which takes an array as input and outputs an object with: The first element of the array as the key of the object. The last element of the array as the value of that key. For example, if the inpu ...