NodeJS显示网站布局而不是预期的JSON数据

时间:2018-10-29 10:39:34

标签: node.js

我有一个在浏览器中管理的管理仪表板。然后,我想为我的移动应用程序提供一个URL端点,以便在那里显示数据。

这是我的app.js的样子:

const PORT = process.env.PORT || 5000;
const env = process.env.NODE_ENV || 'development';

const express = require('express'),
      path = require('path'),
      mongoose = require('mongoose'),
      bodyParser = require('body-parser'),
      passport = require('passport');

const config = require('./backend/config/db')[env]; // load db
mongoose.connect(config.DB, { useNewUrlParser: true }).then(
    () => {console.log('Database is connected') },
    err => { console.log('Can not connect to the database'+ err)}
);

const users = require('./backend/routes/user'); 
const categories = require('./backend/routes/category'); 
const meetups = require('./backend/routes/meetup'); 

const app = express();

// Serve static files from the React app -- added because of Heroku, the local app doesn't need this
app.use(express.static(path.join(__dirname, 'frontend/build')));


app.use(passport.initialize());
require('./backend/passport')(passport);

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

// routes
app.use('/api/users', users);
app.use('/api/categories', categories);
app.use('/api/meetups', meetups);

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

app.listen(PORT, () => {
    console.log(`Server is running on PORT ${PORT}`);
});

backend/routes/meetup中,我有以下代码:

...
// on this route, I want to display the JSON endpoint for the mobile app
router.get('/all', function(req, res) {
    console.log('in /all_meetups');

    Meetup.find({}).sort('-createdAt').exec(function(err, fetched_meetups) {
        if(err) throw err;
        console.log(fetched_meetups);

        let meetups = {
            meetups: fetched_ meetups
        };
        res.json(meetups);
    });

});

router.post('/create', function(req, res) {
  ...
}

router.post('/search', function(req, res) {
  ...
}
...

当我访问URL https://generated_name.herokuapp.com/api/meetups/all时,看到的是网站(管理控制台)布局,而不是移动应用程序的JSON数据。

如何在该URL而非网站布局上显示JSON结构化数据?

0 个答案:

没有答案