Chance
Chance

Reputation: 1654

Conflict of routes in express

I'm building the documentation for my API, I created a route for it as localhost:4000/docs, but for some reason when typinglocalhost:4000 the browser loads the contents of the documentation, I'm forgetting something else I can not imagine anything.

app.js

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

require('./middlewares/md-bootConfig')(app)//start config
require('./routes/Login')(app) //route login
require('./routes/Home')(app) //route home
require('./routes/Doc')(app) //route documents api

module.exports = app

static folder

const bodyParser = require('body-parser')
const logger = require('morgan')
const path = require('path')
const express =  require('express')
module.exports = app => {
    app.use(express.static(path.join(__dirname, '../../../docs')))
    app.set('json spaces', 2)
    app.use(bodyParser.urlencoded({extended: false}));
    app.use(bodyParser.json());
    app.use(logger('dev'));
}

route /docs

const path = require('path')
module.exports = app => {
    app.get('/docs', (req, res) => {
        res.sendFile(path.join(__dirname, '../../../docs', 'index.html'))
    })
}

the docs folder is at the root of the project.

path /

const env = process.env.NODE_ENV
module.exports = app => {
    app.get('/', (req, res) =>{
        res.send({
            version: '1.0.0',
            status: 'API on-line',
            environment: env
        })
    })
}

How do I enter localhost:4000 and load the JSON status from the API and not the load /docs?

Upvotes: 0

Views: 313

Answers (1)

Steven Spungin
Steven Spungin

Reputation: 29071

The response is falling back to your static route.

app.use(express.static(path.join(__dirname, '../../../docs')))

Add an additional route for '/'

app.get('/', (req, res) => {
})

Make sure this route is defined BEFORE your app.use(express.static line

Upvotes: 2

Related Questions