adi
adi

Reputation: 191

Call function and receive JSON

I need to bulid web server with two file activated (student.js) and active (index.js) that I call to a function from the index.js and want to get JSON from the student.js.

I created an outer json that holds all the details of the student and I want to print the JSON to my localhost.

I receive an error when I run it on localhost:

Cannot GET /getAllExcellenceStudent

index.js

var express=require('express');
var app=express();
var port=process.env.PORT || 3000;
var student=require('./student');
var stud=require('./students');

student.AllExcellenceStudent;

app.listen(port);
console.log('listening on port'+port);

student.js

var express=require('express');
var app=express();
var stud=require('./student');

module.exports = function AllExcellenceStudent() {
    app.get('/getAllExcellenceStudent/', function(req, res) {
         res.json.parse({name:'adi'});
    })
};

students.json

{
    "students": [
        { "name": "John",  "grade": "90", "year": "2005", "coures": "math"     },
        { "name": "Anna",  "grade": "80", "year": "2000", "coures": "sport"    },
        { "name": "Peter", "grade": "75", "year": "2005", "coures": "math"     },
        { "name": "ron",   "grade": "70", "year": "2000", "coures": "computer" },
        { "name": "mor",   "grade": "85", "year": "2005", "coures": "computer" }
    ]
}

Upvotes: 3

Views: 63

Answers (1)

Nick D
Nick D

Reputation: 1493

You need to pass your app object to your student and students module:

index.js:

var express=require('express');
var app=express();
var port=process.env.PORT || 3000;
var student=require('./student');
var stud=require('./students');

student(app); // pass your app variable, no need for the function name

app.listen(port);
console.log('listening on port'+port);

student.js:

module.exports = function (app) {
    app.get('/getAllExcellenceStudent/', function(req, res) {
         res.json.parse({name:'adi'});
    })
};

Upvotes: 1

Related Questions