【问题标题】:How to make NodeJS routes on subdomain?如何在子域上制作 NodeJS 路由?
【发布时间】:2021-03-26 01:01:04
【问题描述】:

不适用于子域上的 NodeJs 路由。 当我向子域提出请求时/我得到了很好的响应“主域 - 主页” 但是当我向子域/关于我提出请求时,我得到了错误

这是我的代码:

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.

【问题讨论】:

  • 这能回答你的问题吗? stackoverflow.com/questions/30951466/…
  • 您应该将错误文本添加到您的问题中!
  • 您能与我分享您的子域网址吗?我在节点上工作,表达子域。我可以帮你。

标签: node.js express routes subdomain


【解决方案1】:

你可以使用轻量级包

node-awesome-router

对于每个路由,您可以指定您的自定义子域

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: [...]
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-22
    • 2015-10-01
    • 2013-12-14
    • 1970-01-01
    • 2015-08-20
    • 2011-07-18
    • 2017-10-13
    • 2011-05-12
    相关资源
    最近更新 更多