邮递员401未经授权|不能获取

时间:2019-02-13 10:56:53

标签: node.js post get routes postman

我正在写教程,但无法获得与https://getbootstrap.com/docs/4.3/utilities/flex/#order部分相同的结果

我可以通过邮递员邮寄,但不能GET。

这是我得到的快照。

https://youtu.be/qmNPpoVkY2Y?t=1238

server / index.js

import express from 'express';
import dbConfig from './config/db';
import middlewareConfig from './config/middlewares';
import { MeetupRoutes } from './modules';

const app = express();

/**
*Database
*/
dbConfig();
/**
*Middlewares
*/
middlewareConfig(app);

app.use('/api', [MeetupRoutes]);

const PORT = process.env.PORT || 3000;

app.listen(PORT, err => {
  if (err) {
    console.error(err);
  }{
    console.log('App listen to port: ${PORT}');
  }
});

db.js

import mongoose from 'mongoose';

export default () => {
  mongoose.Promise = global.Promise;
  mongoose.connect('mongodb://localhost/meetupME');
  mongoose.connection
    .once('open', () => console.log('Mongobd running'))
    .on('error', err => console.error(err))

};

middleware.js

import bodyParser from 'body-parser';
import morgan from 'morgan';

export default app => {
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false}));
  app.use(morgan('dev'));
};

meetups / controller.js

import Meetup from './model';

export const createMeetup = async (req,res) => {
  const { title, description } = req.body;
  const newMeetup = new Meetup({ title, description });

  try {
    return res.status(201).json({ meetup: await newMeetup.save() });
  } catch (e) {
    return res.status(e.status).json({ error: true, message: 'Error with Meetup' });
  }
}

export const getAllMeetups = async (req, res) => {
  try {
    return res.status(200).json({ meetups: await Meetup.find({} )});
  } catch (e) {
    return res.status(e.status).json({ error: true, message: 'Error with Meetup' });
}
}

meetup / index.js

import MeetupRoutes from './routes';

export { MeetupRoutes };

meetup / model.js

import mongoose, { Schema } from 'mongoose';

const MeetupSchema = new schema({
  title: {
    type: String,
    required: true
  },
  description: {
    type: String,
    required: true
  }
});

export default mongoose.model('Meetup', MeetupSchema);

meetup / routers.js

import { Router } from 'express';
import * as MeetupController from './controller'

const routes = new Router();

routes.post('/meetups', MeetupController.createMeetup);
routes.get('/meetups', MeetupController.getAllMeetups);

export default routes;

我尝试检查拼写错误,但是由于某些原因,我无法使其正常工作。谢谢!

这也是邮递员导出的请求:

GET /api/meetups HTTP/1.1
Host: localhost:3000
Content-Type: application/x-www-form-urlencoded
cache-control: no-cache
Postman-Token: e912d6ac-d9b6-465d-88e8-4aee65a906fa
title=title1description=This+is+a+descriptionundefined=undefined

2 个答案:

答案 0 :(得分:0)

您是否尝试将Postman上的 x-www-form-urlencoded 更改为原始? 同时选择类型 application / json

答案 1 :(得分:0)

尝试使用Curl

curl -X GET your_usl?param=whatever