Using Postman to test my node.js program

16,721

use POSTMAN with Raw header. here is my post test

var express = require('express');
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/usersdb');
var bodyParser = require('body-parser');
var app = express();

//Third-party middelware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

app.post('/users', function (req, res) {
    console.log(req.body);
    res.status(200).send(req.body);
});

var port = process.env.PORT || 3000;
app.listen(port);
console.log('Listening on http://localhost:' + port);

enter image description here

Share:
16,721
jensiepoo
Author by

jensiepoo

...

Updated on June 04, 2022

Comments

  • jensiepoo
    jensiepoo almost 2 years

    I'm using Chrome's extension Postman to test my Node.js(express module) program. Basically, I want my program to allow user input on Postman and retrieves information from somewhere in my program based on the user input.

    So the program takes in my user input via Postman(raw code), say

        [{ID:0, image:tiger.jpg},
         {ID:1, image:cat.jpg},
         {ID:2, image:dog.jpg}]
    

    Then my code will process the user input's ID's only(regardless of the images), and get the string of objects associated with these 3 ID's. After getting the string, my program will send an HTTP request to print the objects retrieved to my localhost server. How would I be able to achieve this using express's POST and GET method. When to use post/get? Do I use post to receive input? and use get to retrieve data from program?

    Below are some functions I was thinking of including..

        app.post('/', express.bodyParser(), function (req, res) {
    

    Someone suggested this. Can someone tell me if whether or not this function can receive input from Postman? I noticed this method might change req.body? But I don't really understand how it changes and parses the input.

    There were way too many questions, and I apologize in advance. Basically, I just need to know how to write the program given that description, and I shall figure out the rest myself!

    Thanks!