0

Doesn't work NodeJs routes on subdomain. When I make request to subdomain/ I got ok response 'Main domain - Homepage' but when I make request to subdomain/about I got error

Here is my code:

const app = express()
const bodyParser = require('body-parser')
const jwt = require("jsonwebtoken");
const path = require("path");
const server = require('http').createServer(app);
const db = require('./db/db');
const subdomain = require('express-subdomain')
var config = require('./config');

const port = 3000

app.get('/', (req, res) => {
res.send("Main domain - Homepage")
})

app.get('/about', (req, res) => {
res.send("Main domain - About")
})

server.listen(30000, function(){ console.log('Server started on ' + server.address().port); });```

Error is

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@subdomain to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
Mirzet Zukic
  • 31
  • 2
  • 6

1 Answers1

0

You can use lightweight package

node-awesome-router

For Each Route you can specify your custom subdomain

const NodeAwesomeRouter = require('node-awesome-router')
const app = require('express')()
// GET ->  USERS.site.com/user/info/1
const routes = {
 key: '/user', 
 routes: {
  '/info/:id?' () {
    return {
      method: 'get',
      async handler (req, res) {
       // your logic here
       return res.status(200).send('welcome')
      }   
    }
 }
}, 
 subdomain: 'users' 
}
NodeAwesomeRouter({
 app,
 routes: [routes, anotherRoutes ],
 middlewares: [...]
})
Kandrat
  • 464
  • 7
  • 16