node.js - Connection Error (Couldnt get any Response) unable to post on Mongodb -


i have tried post in mongodb using postman while posting text got error of (couldnt response) not showing error command nodemon please me did mistake ..! need ?

my index.js file is:-

const express = require('express'); const path = require('path'); const bodyparser = require('body-parser'); const cors = require('cors'); const mongoose = require('mongoose');  const config = require('./configdb/database');  // connection database mongoose.connect(config.database);  // connection success db mongoose.connection.on('connected',() => {     console.log('connected database ' +config.database); });  //on error while connecting mongoose.connection.on('error',(err) => {     console.log('connection error try again database failed connect ' +err); });  const app = express(); const articles = require('./routers/articles');  // port start  const port = 2200;  // cors middleware app.use(cors());  // set static public folder app.use(express.static(path.join(__dirname, 'public')));  // body parser middleware app.use(bodyparser.urlencoded({ extended: false })) app.use(bodyparser.json());  app.use('/articles',articles);  // index route app.get('/', (req, res) => {     res.send('this initial page starting session') });  app.listen(port, () => {     console.log('server started in' + port) }) 

my articles.js file

const express = require('express'); const router = express.router(); const config = require('../configdb/database'); const article = require('../models/article');  // register of article router.post('/new-article', (req,res,next) => {      let article = new article();     article.title = req.body.title;      console.log(req.body.title);     return;      article.save(function(err){         if(err){               res.json({success: false, msg: 'failed register article' });          } else {             res.json({success: true, msg: 'new article registered'});         }     });  });  module.exports = router; 

my article.js file

const mongoose = require('mongoose'); const config = require('../configdb/database');  const articleschema = mongoose.schema({     title:{         type: string,     } });  const article = module.exports = mongoose.model('article', articleschema) 

but have got message article.title = req.body.title; , error follows:-

postman , nodemon screenshot

in articles.js have return function after displaying title cause problem!

// register of article router.post('/new-article', (req, res, next) => {      let article = new article();     article.title = req.body.title;      console.log(req.body.title);     // return;      article.save(function (err) {         if (err) {             res.json({                 success: false,                 msg: 'failed register article'             });          } else {             res.json({                 success: true,                 msg: 'new article registered'             });         }     });  }); 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -