. Advertisement .
..3..
. Advertisement .
..4..
Here is the program I run:
var express= require('express');
var bodyParser= require('body-parser');
var morgan = require('morgan');
var config=require('./config');
var app= express();
var mongoose=require('mongoose');
//var User=require('./database/user')
mongoose.connect('mongodb://localhost:27017/db',function(err){
if(err){
console.log(err);
}
else{
console.log("connected!");
}
});
app.use(bodyParser.urlencoded({extended: true })); //if false then parse only strings
app.use(bodyParser.json());
app.use(morgan('dev'));//log all the requests to the console
var api=require('./app/routes/api')(app,express);
app.use('/api',api);
app.get('*',function(req,res){
res.sendFile(__dirname + '/public/views/index.html');
}); // * means any route
app.listen(config.port,function(err){
if(err){enter code here
console.log(err);
}
else{
console.log("The server is running");
}
});
module.exports = router;
var User = require('../models/user');
var Event = require('../models/event');
var config = require('../../config');
var secret = config.secretKey;
module.exports = function (app, express) {
var api = express.Router();
app.use()
api.post('/signup', function (req, res) {
var user = new User({
name: req.body.name,
username: req.body.username,
password: req.body.password
});
user.save(function (err) {
if (err) {
res.send(err);
return;
}
res.json({
message: 'User created!'
});
});
});
api.get('/users', function (req, res) {
User.find({}, function (err, users) {
if (err) {
res.send(err);
return;
}
res.json(users);
});
});
api.post('/eventfeed', function (req, res) {
var event = new Event({
name: req.body.name,
location: req.body.location,
description: req.body.description,
price: req.body.price,
rating: req.body.rating
});
event.save(function (err) {
if (err) {
res.send(err);
return;
}
res.json({
message: 'Event created!'
});
});
});
// utility function for sorting an array by a key in alpha order
api.get('/sortby_price', function (err) {
if (err) return err;
// utility function for sorting an array by a key in parsed numeric order
else {
function sortArrayNum(arr, key) {
arr.sort(function (a, b) {
return parseInt(a[key], 10) - parseInt(b[key], 10);
});
}
var dicts = EventSchema.saved;
for (var i = 0; i < dicts.length; i++) {
var terms = dicts[i].terms;
sortArrayNum(terms, "price");
}
}
return api;
});
}
After I run, it returns an error:
TypeError: app.use() requires middleware functions
at EventEmitter.use (c:\Users\MY APY\WebstormProjects\Main\node_modules\express\lib\application.js:209:11)
at module.exports (c:\Users\MY LAPY\WebstormProjects\Main\app\routes\api.js:10:9)
at Object. (c:\Users\MY LAPY\WebstormProjects\Main\server.js:20:36)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Does anyone have any suggestions for the problem below: app.use() requires a middleware function in the javascript – How to correct it?
The cause:
You have got this error because in your api.js the line of number 10 is not correct:
A function with 3 parameters is required for app.use:
Solution:
You can do as the following in the case you want to use this middleware by only few chosen paths:
This was the problem I faced when I left.
In my Routes.js. We need to export all routes.
In my server.js I had
Check your ‘app/routes/api” file to verify that it is exportable.