简体   繁体   中英

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" }
    ]
}

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'});
    })
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM