Express request doesn't end












1















i made a very simple api using express.js. Here's the code:



var express = require('express');
var app = express();
var morgan = require('morgan');
var UserModel = require('../Models/User')


app.use(morgan('short'));
app.use(express.json());


app.get('/getAll', (req, res) => {
res.status(200).json({auth: true});
})

app.post('/addUser', (req, res) => {
const { name, email, password } = req.body;
UserModel.create({name, email, password}, (err, user) => {
if(err) return res.status(500).end({auth: false})
res.status(200).end({user});
});

});

module.exports = app;


And here's the userModel:



const mongoose = require("mongoose")
const Schema = mongoose.Schema;

const UserSchema = new Schema(
{
name: String,
email: String,
password: String,
},
{timestamps: false}
);
mongoose.model("User", UserSchema);

module.exports = mongoose.model("User");


This is the main server.js file:



var express = require('express');
var app = express();

const AuthController = require("./Controllers/AuthController");
const PORT = 3001;

app.use("/api/auth", AuthController);


app.listen(PORT, () => console.log(`Listening on port ${PORT}..`))


This is the db.js file:



const mongoose = require('mongoose');


const dbRoute = "mongodb://<user>:<password>@<stuff>/nodejs-db";

mongoose.connect(
dbRoute,
{useNewUrlParser: true}
);


So here's the problem. when i try to make a request to this api using Insomnia, the requests doesn't end. Basically Insomia starts sending the request and i have to wait like 20 secs until i get something on my express terminal. If i end the request manually i get this:



::ffff:127.0.0.1 - POST /api/auth/addUser HTTP/1.1 - - - - ms


I tried looking online but couldn't find anything useful.
I come from a django backgrond. I'm new to Node and Express js.



Edit:
The problem is only with the posts requests because whenever i make a get request it returns immediately {auth: true}.










share|improve this question

























  • express() should only be used for the main server, otherwise take express.Router().

    – Jonas Wilms
    Nov 25 '18 at 11:25













  • changed app = express.Router() and also all the requests but the problem persists

    – Abel Hristodor
    Nov 25 '18 at 11:30











  • How do you establish the connection to mongodb?

    – Jonas Wilms
    Nov 25 '18 at 11:36











  • using db.js - added now

    – Abel Hristodor
    Nov 25 '18 at 11:39











  • where do you require("./db") ?

    – Jonas Wilms
    Nov 25 '18 at 12:13
















1















i made a very simple api using express.js. Here's the code:



var express = require('express');
var app = express();
var morgan = require('morgan');
var UserModel = require('../Models/User')


app.use(morgan('short'));
app.use(express.json());


app.get('/getAll', (req, res) => {
res.status(200).json({auth: true});
})

app.post('/addUser', (req, res) => {
const { name, email, password } = req.body;
UserModel.create({name, email, password}, (err, user) => {
if(err) return res.status(500).end({auth: false})
res.status(200).end({user});
});

});

module.exports = app;


And here's the userModel:



const mongoose = require("mongoose")
const Schema = mongoose.Schema;

const UserSchema = new Schema(
{
name: String,
email: String,
password: String,
},
{timestamps: false}
);
mongoose.model("User", UserSchema);

module.exports = mongoose.model("User");


This is the main server.js file:



var express = require('express');
var app = express();

const AuthController = require("./Controllers/AuthController");
const PORT = 3001;

app.use("/api/auth", AuthController);


app.listen(PORT, () => console.log(`Listening on port ${PORT}..`))


This is the db.js file:



const mongoose = require('mongoose');


const dbRoute = "mongodb://<user>:<password>@<stuff>/nodejs-db";

mongoose.connect(
dbRoute,
{useNewUrlParser: true}
);


So here's the problem. when i try to make a request to this api using Insomnia, the requests doesn't end. Basically Insomia starts sending the request and i have to wait like 20 secs until i get something on my express terminal. If i end the request manually i get this:



::ffff:127.0.0.1 - POST /api/auth/addUser HTTP/1.1 - - - - ms


I tried looking online but couldn't find anything useful.
I come from a django backgrond. I'm new to Node and Express js.



Edit:
The problem is only with the posts requests because whenever i make a get request it returns immediately {auth: true}.










share|improve this question

























  • express() should only be used for the main server, otherwise take express.Router().

    – Jonas Wilms
    Nov 25 '18 at 11:25













  • changed app = express.Router() and also all the requests but the problem persists

    – Abel Hristodor
    Nov 25 '18 at 11:30











  • How do you establish the connection to mongodb?

    – Jonas Wilms
    Nov 25 '18 at 11:36











  • using db.js - added now

    – Abel Hristodor
    Nov 25 '18 at 11:39











  • where do you require("./db") ?

    – Jonas Wilms
    Nov 25 '18 at 12:13














1












1








1








i made a very simple api using express.js. Here's the code:



var express = require('express');
var app = express();
var morgan = require('morgan');
var UserModel = require('../Models/User')


app.use(morgan('short'));
app.use(express.json());


app.get('/getAll', (req, res) => {
res.status(200).json({auth: true});
})

app.post('/addUser', (req, res) => {
const { name, email, password } = req.body;
UserModel.create({name, email, password}, (err, user) => {
if(err) return res.status(500).end({auth: false})
res.status(200).end({user});
});

});

module.exports = app;


And here's the userModel:



const mongoose = require("mongoose")
const Schema = mongoose.Schema;

const UserSchema = new Schema(
{
name: String,
email: String,
password: String,
},
{timestamps: false}
);
mongoose.model("User", UserSchema);

module.exports = mongoose.model("User");


This is the main server.js file:



var express = require('express');
var app = express();

const AuthController = require("./Controllers/AuthController");
const PORT = 3001;

app.use("/api/auth", AuthController);


app.listen(PORT, () => console.log(`Listening on port ${PORT}..`))


This is the db.js file:



const mongoose = require('mongoose');


const dbRoute = "mongodb://<user>:<password>@<stuff>/nodejs-db";

mongoose.connect(
dbRoute,
{useNewUrlParser: true}
);


So here's the problem. when i try to make a request to this api using Insomnia, the requests doesn't end. Basically Insomia starts sending the request and i have to wait like 20 secs until i get something on my express terminal. If i end the request manually i get this:



::ffff:127.0.0.1 - POST /api/auth/addUser HTTP/1.1 - - - - ms


I tried looking online but couldn't find anything useful.
I come from a django backgrond. I'm new to Node and Express js.



Edit:
The problem is only with the posts requests because whenever i make a get request it returns immediately {auth: true}.










share|improve this question
















i made a very simple api using express.js. Here's the code:



var express = require('express');
var app = express();
var morgan = require('morgan');
var UserModel = require('../Models/User')


app.use(morgan('short'));
app.use(express.json());


app.get('/getAll', (req, res) => {
res.status(200).json({auth: true});
})

app.post('/addUser', (req, res) => {
const { name, email, password } = req.body;
UserModel.create({name, email, password}, (err, user) => {
if(err) return res.status(500).end({auth: false})
res.status(200).end({user});
});

});

module.exports = app;


And here's the userModel:



const mongoose = require("mongoose")
const Schema = mongoose.Schema;

const UserSchema = new Schema(
{
name: String,
email: String,
password: String,
},
{timestamps: false}
);
mongoose.model("User", UserSchema);

module.exports = mongoose.model("User");


This is the main server.js file:



var express = require('express');
var app = express();

const AuthController = require("./Controllers/AuthController");
const PORT = 3001;

app.use("/api/auth", AuthController);


app.listen(PORT, () => console.log(`Listening on port ${PORT}..`))


This is the db.js file:



const mongoose = require('mongoose');


const dbRoute = "mongodb://<user>:<password>@<stuff>/nodejs-db";

mongoose.connect(
dbRoute,
{useNewUrlParser: true}
);


So here's the problem. when i try to make a request to this api using Insomnia, the requests doesn't end. Basically Insomia starts sending the request and i have to wait like 20 secs until i get something on my express terminal. If i end the request manually i get this:



::ffff:127.0.0.1 - POST /api/auth/addUser HTTP/1.1 - - - - ms


I tried looking online but couldn't find anything useful.
I come from a django backgrond. I'm new to Node and Express js.



Edit:
The problem is only with the posts requests because whenever i make a get request it returns immediately {auth: true}.







javascript node.js express






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 11:46







Abel Hristodor

















asked Nov 25 '18 at 11:12









Abel HristodorAbel Hristodor

125




125













  • express() should only be used for the main server, otherwise take express.Router().

    – Jonas Wilms
    Nov 25 '18 at 11:25













  • changed app = express.Router() and also all the requests but the problem persists

    – Abel Hristodor
    Nov 25 '18 at 11:30











  • How do you establish the connection to mongodb?

    – Jonas Wilms
    Nov 25 '18 at 11:36











  • using db.js - added now

    – Abel Hristodor
    Nov 25 '18 at 11:39











  • where do you require("./db") ?

    – Jonas Wilms
    Nov 25 '18 at 12:13



















  • express() should only be used for the main server, otherwise take express.Router().

    – Jonas Wilms
    Nov 25 '18 at 11:25













  • changed app = express.Router() and also all the requests but the problem persists

    – Abel Hristodor
    Nov 25 '18 at 11:30











  • How do you establish the connection to mongodb?

    – Jonas Wilms
    Nov 25 '18 at 11:36











  • using db.js - added now

    – Abel Hristodor
    Nov 25 '18 at 11:39











  • where do you require("./db") ?

    – Jonas Wilms
    Nov 25 '18 at 12:13

















express() should only be used for the main server, otherwise take express.Router().

– Jonas Wilms
Nov 25 '18 at 11:25







express() should only be used for the main server, otherwise take express.Router().

– Jonas Wilms
Nov 25 '18 at 11:25















changed app = express.Router() and also all the requests but the problem persists

– Abel Hristodor
Nov 25 '18 at 11:30





changed app = express.Router() and also all the requests but the problem persists

– Abel Hristodor
Nov 25 '18 at 11:30













How do you establish the connection to mongodb?

– Jonas Wilms
Nov 25 '18 at 11:36





How do you establish the connection to mongodb?

– Jonas Wilms
Nov 25 '18 at 11:36













using db.js - added now

– Abel Hristodor
Nov 25 '18 at 11:39





using db.js - added now

– Abel Hristodor
Nov 25 '18 at 11:39













where do you require("./db") ?

– Jonas Wilms
Nov 25 '18 at 12:13





where do you require("./db") ?

– Jonas Wilms
Nov 25 '18 at 12:13












2 Answers
2






active

oldest

votes


















0














Change your .end() to .send()



app.post('/addUser', (req, res) => {
const { name, email, password } = req.body;
UserModel.create({name, email, password}, (err, user) => {
if(err) return res.status(500).send({auth: false})
res.status(200).send({user});
});

});





share|improve this answer
























  • Changed - problem persists

    – Abel Hristodor
    Nov 25 '18 at 11:43



















0














I solved this problem.
Apparently the problem was that my db connection was on another file.
All i did was to move the content from the db.js file to my server.js file.
I forgot to include my db file.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466880%2fexpress-request-doesnt-end%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Change your .end() to .send()



    app.post('/addUser', (req, res) => {
    const { name, email, password } = req.body;
    UserModel.create({name, email, password}, (err, user) => {
    if(err) return res.status(500).send({auth: false})
    res.status(200).send({user});
    });

    });





    share|improve this answer
























    • Changed - problem persists

      – Abel Hristodor
      Nov 25 '18 at 11:43
















    0














    Change your .end() to .send()



    app.post('/addUser', (req, res) => {
    const { name, email, password } = req.body;
    UserModel.create({name, email, password}, (err, user) => {
    if(err) return res.status(500).send({auth: false})
    res.status(200).send({user});
    });

    });





    share|improve this answer
























    • Changed - problem persists

      – Abel Hristodor
      Nov 25 '18 at 11:43














    0












    0








    0







    Change your .end() to .send()



    app.post('/addUser', (req, res) => {
    const { name, email, password } = req.body;
    UserModel.create({name, email, password}, (err, user) => {
    if(err) return res.status(500).send({auth: false})
    res.status(200).send({user});
    });

    });





    share|improve this answer













    Change your .end() to .send()



    app.post('/addUser', (req, res) => {
    const { name, email, password } = req.body;
    UserModel.create({name, email, password}, (err, user) => {
    if(err) return res.status(500).send({auth: false})
    res.status(200).send({user});
    });

    });






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 25 '18 at 11:40









    Dinesh PandiyanDinesh Pandiyan

    2,530925




    2,530925













    • Changed - problem persists

      – Abel Hristodor
      Nov 25 '18 at 11:43



















    • Changed - problem persists

      – Abel Hristodor
      Nov 25 '18 at 11:43

















    Changed - problem persists

    – Abel Hristodor
    Nov 25 '18 at 11:43





    Changed - problem persists

    – Abel Hristodor
    Nov 25 '18 at 11:43













    0














    I solved this problem.
    Apparently the problem was that my db connection was on another file.
    All i did was to move the content from the db.js file to my server.js file.
    I forgot to include my db file.






    share|improve this answer




























      0














      I solved this problem.
      Apparently the problem was that my db connection was on another file.
      All i did was to move the content from the db.js file to my server.js file.
      I forgot to include my db file.






      share|improve this answer


























        0












        0








        0







        I solved this problem.
        Apparently the problem was that my db connection was on another file.
        All i did was to move the content from the db.js file to my server.js file.
        I forgot to include my db file.






        share|improve this answer













        I solved this problem.
        Apparently the problem was that my db connection was on another file.
        All i did was to move the content from the db.js file to my server.js file.
        I forgot to include my db file.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 12:12









        Abel HristodorAbel Hristodor

        125




        125






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466880%2fexpress-request-doesnt-end%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks