1

I have written a CRUD using node.js .I need to use another node project as a micro service.So i use the axios npm module for send http request to another node project.When i run the code following error comes again and again.how to fix it. this is my index.js file

const axios = require('axios').default; const express=require('express'); const bodyParser=require('body-parser'); const mysqlConnection=require('./connections'); const cors = require('cors'); mysqlConn = mysqlConnection.startDBConnection(); // console.log(mysqlConn); var app=express(); app.use(bodyParser.json()); const emp=require('./routes/employee')(express.Router(), mysqlConn); app.use('/employeeDetails',emp); app.listen(3001,()=>console.log('express server is working at port:3001'));

This is the routes files. employee.js

const apiAdapter = require('./apiAdapter'); const BASE_URL = 'http://localhost:3000'; const api = apiAdapter(BASE_URL); module.exports = (router, mysqlConnection) => { router.get('/employees/:id', (req, res) => { mysqlConnection.query('SELECT * FROM Employee WHERE Id = ?', [req.params.Id], (err, rows, fields) => { if (!err) res.send(fields); else console.log(err); }) api.get('/checkAudtStatus',req.body) .then(respond) { if(respond.data===true) { api.post('/createAuditRecord',req.body) .then(respond) { res.send(res.data) } } } }) router.post('/employees',(req,res)=>{ emp = { "Id":req.body.Id, "Name":req.body.Name, "Age":req.body.Age, "Salary":req.body.Salary, "City":req.body.City } mysqlConnection.query("INSERT INTO Employee SET ?",emp, function(err, result){ // //If err, callback false if(err){ res.send(false); // res.send(false); } else{ res.send(result); } }) api.get('/checkAuditStatus',req.body) .then(respond) { if(respond.data===true) { api.post('/createAuditRecord',req.body) .then(respond) { res.send(res.data) } } } }) }

This is the apiAdapter.js

const axios = require('axios'); module.exports = (baseURL) => { //allowing to handle other micro-services' urls by gateway return axios.create({ baseURL: baseURL, }); }

This is the connection.js

var mysql = require('mysql'); //startDBConnection method function startDBConnection(){ try{ var mysqlConnection = mysql.createPool({ host: 'localhost', user: 'root', connectionLimit:10,//port:'8080', password: '', database: 'Test', //multipleStatements: true }); return mysqlConnection; } catch(e){ console.log(e); } } module.exports.startDBConnection = startDBConnection;

following error comes while i run the above code.

[nodemon] app crashed - waiting for file changes before starting... [nodemon] restarting due to changes... [nodemon] starting `node index.js` /home/naoda/Documents/test_/node_modules/express/lib/router/index.js:458 throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) ^ TypeError: Router.use() requires a middleware function but got a undefined at Function.use (/home/naoda/Documents/test_/node_modules/express/lib/router/index.js:458:13) at Function.<anonymous> (/home/naoda/Documents/test_/node_modules/express/lib/application.js:220:21) at Array.forEach (<anonymous>) at Function.use (/home/naoda/Documents/test_/node_modules/express/lib/application.js:217:7) at Object.<anonymous> (/home/naoda/Documents/test_/index.js:17:5) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) [nodemon] app crashed - waiting for file changes before starting...

2 Answers 2

2

when executing the app.use(bodyParser.json()); couldn't find the value for bodyParser.json() so that try executing app.use(bodyparser.urlencoded({extended:false})); before executing app.use(bodyParser.json());

Sign up to request clarification or add additional context in comments.

Comments

1

In these two lines of code:

const emp=require('./routes/employee')(express.Router(), mysqlConn); app.use('/employeeDetails',emp); 

emp needs to be a middleware function (or a router will also work as a middleware function). But, when you look into the details of your employee module, you export a function that, when called doesn't return anything. So, emp is undefined. Probably, what you need to return from emp is the router you're adding routes to.

It appears you also have a problem with how your api.get().then() code is declared too. Combining those two fixes, would be like this:

const apiAdapter = require('./apiAdapter'); const BASE_URL = 'http://localhost:3000'; const api = apiAdapter(BASE_URL); module.exports = (router, mysqlConnection) => { router.get('/employees/:id', (req, res) => { mysqlConnection.query('SELECT * FROM Employee WHERE Id = ?', [req.params.Id], (err, rows, fields) => { if (!err) { res.send(fields); } else { // send some sort of error status console.log(err); res.sendStatus(500); return; } }); api.get('/checkAudtStatus', req.body).then(respond => { if (respond.data === true) { api.post('/createAuditRecord', req.body).then(res => { res.send(res.data); }); } }); }); return router; } 

Other unrelated problems that also need fixing:

  1. api.get() has no error handler if that promise rejects
  2. You have a code path where you will try to call res.send() twice for the same request. Once in the callback for your mysqlConnnection.query() and once in the .then() handler for api.get().then()
  3. Same declaration problem with the api.post().then().

1 Comment

Thank you for this reply and i supported me lot

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.