Home » Javascript » javascript – Why I don't get any response?

javascript – Why I don't get any response?

Posted by: admin February 24, 2020 Leave a comment

Questions:

I am learning NodeJS with express now. This is my server:

const express = require('express');
const helmet = require('helmet');
const router = express.Router();
const response = require('./network/response')
var app = express();
app.use(helmet());
app.use(express.json());
app.use(express.urlencoded({ extended: false }))
app.use(router);


router.get('/message', (req, res) => {
    response.success(req, res, `Lista de mensajes 1000`);
})

router.post('/message', (req, res) => {

    if (req.query.error == 'ok') {
        response.error(req, res, `Error simulado`, 401)
    } else {
        response.success(req, res, `Creado correctamente`, 200);
    }

})

router.delete('/message', (req, res) => {
    res.send(`Mensaje eliminado`);
})

app.listen(3000, () => {
    console.log(`La aplicacion se esta escuchando en puerto 3000`);
})

and this is my network module:

exports.success = function (req, res, message, status) {
    res.status(status || 200).send({
        error: '',
        body: message
    });
}

exports.error = function (req, res, message, status) {
    res.status(status || 500).send({
        error: message,
        body: ''
    });
}

network module help me to have a better control of HTTP request. The problem is that when I make a POST request I never get the response, is just loading and loading. I am trying to get the error but nothing. This is the request:

http://localhost:3000/message?error=ok

enter image description here

How to&Answers:

It is working mate.

check these curl requests :

curl -XPOST -data ‘{“data”:”data”}’ http://localhost:3000/message

curl: (3) Port number ended with ‘”‘
{“error”:””,”body”:”Creado correctamente”}

POSTMAN :
enter image description here

curl -XPOST -data ‘{“data”:”data”}’ http://localhost:3000/message?error=ok

curl: (3) Port number ended with ‘”‘

{“error”:”Error simulado”,”body”:””}