简体   繁体   English

使用 express 和 nodejs 提供 Angular 应用程序时无法获取/

[英]cannot GET / when serving Angular app using express and nodejs

I am trying to serve an angular app using nodejs.我正在尝试使用 nodejs 提供一个有角度的应用程序。 But i get this error "Cannot GET /" in the body of the page.但是我在页面正文中收到此错误“无法获取 /”。 I tried a number of things but still this does not work.我尝试了很多东西,但这仍然不起作用。 do you folks have any suggestion?你们有什么建议吗?

const express = require('express')
const app = express()
var cors = require('cors')
const bodyParser = require('body-parser')
const fileUpload = require('express-fileupload')

const couchDb = require('./modules/couchDb')
const db = couchDb.db
const schedules = require('./modules/schedules')
const stations = require('./modules/stations')
const testConfigs = require('./modules/testConfigs')

app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(fileUpload())

app.listen(5000, () => console.log('Listening on port 5000'))


////////////////////////////////////////
// View
////////////////////////////////////////
const viewOptions = { include_docs: true }
app.route('/api/schedules').get((req, res) => {
    couchDb.getType('schedule', viewOptions).then(docs => {
        res.send(docs)
    }).catch(err => {
        console.log(err)
        res.send({})
    })
})

app.route('/api/stations').get((req, res) => {
    couchDb.getType('station', viewOptions).then(docs => {
        res.send(docs)
    }).catch(err => {
        console.log(err)
        res.send({})
    })
})

app.route('/api/tests').get((req, res) => {
    couchDb.getType('testConfig', viewOptions).then(docs => {
        res.send(docs)
    }).catch(err => {
        console.log(err)
        res.send({})
    })
})

you are missing your routes eg你错过了你的路线,例如

app.get('/', function (req, res) {
      res.send('hello world')
  })

or you need to include your all routes through middle ware.或者您需要通过中间件包含所有路线。

You are getting that error because you are not declaring any endpoints or telling the server to serve anything.您收到该错误是因为您没有声明任何端点或告诉服务器提供任何服务。 It is listening on port 5000, but no responses to any urls have been defined.它正在侦听端口 5000,但没有定义对任何 url 的响应。 Here is a piece of example code that will resolve your issue.这是一段可以解决您的问题的示例代码。

const express = require('express')
const app = express()
var cors = require('cors')
const bodyParser = require('body-parser')
const fileUpload = require('express-fileupload')


app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(fileUpload())

// This block will make it so that every path on port 5000 responds with "Hello, World!"
app.get('*', (req, res) => {
    res.status(200).send("Hello, World!");
});

app.listen(5000, () => console.log('Listening on port 5000'))

This will make it respond with basic text, if you want to serve an angular application, you will need to look into serving static content from express: https://expressjs.com/en/starter/static-files.html这将使其响应基本文本,如果您想提供有角度的应用程序,则需要研究从 express 提供静态内容: https : //expressjs.com/en/starter/static-files.html

You have to use a routing middleware and map your modules to the required modules.Also make sure your modules are mounted in router instance.您必须使用路由中间件并将您的模块映射到所需的模块。还要确保您的模块安装在路由器实例中。

Something like就像是

const express = require('express')
const app = express()
var cors = require('cors')
const bodyParser = require('body-parser')
const fileUpload = require('express-fileupload')

const couchDb = require('./modules/couchDb')
const db = couchDb.db
const schedules = require('./modules/schedules')
const stations = require('./modules/stations')
const testConfigs = require('./modules/testConfigs')

app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(fileUpload())

//All requests with /schedules will go to './modules/schedules' 
app.use('/schedules', schedules);
app.use('/stations', stations);

app.listen(5000, () => console.log('Listening on port 5000'))

your ./modules/station should look like你的 ./modules/station 应该看起来像

var express = require('express')
var router = express.Router()

router.get('/', function (req, res) {
  res.send('You are in /station')
})
router.get('/new', function (req, res) {
  res.send('You are in /station/new')
})

module.exports = router

For more : https://expressjs.com/en/guide/routing.html更多信息: https : //expressjs.com/en/guide/routing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM