In this post we will use Expressjs framework to create Web API that return list of student, and we’ll consume it using AngularJs.

In order to install Nodejs you can see Install & run your first application Nodejs.

In order to install Express you can see First web api application with Express Js.

Create Express application

Install express:

  $ npm install express

Create Web API:
Create app.js file:

 var express = require('express');
    var app = express();

    var students = [

        { id: 1, name: "James Soup", age: 21 },
        { id: 2, name: "Humain yo", age: 20 },
        { id: 3, name: "Hammer tomms", age: 22 }
    ];

    app.get('/api/student', function (req, res) {
        res.json(students);
    });

    app.get('/api/student/:id', function (req, res) {
        var id = req.params.id * 1; // convert to number

        var student = students[id - 1];
        if (student) {
            res.json(student);
        }
        else {
            res.status(404).end();
            res.status(404).send('Sorry, we cannot find that student!');
        }
    });
    app.listen(3000, function () {
        console.log('Student Api listening on http://localhost:3000!');
    });

We create a array that store list of Students.

We defines two Get response that return Students:

‘app.get(‘/api/student’)’ method returns the entire list of students.
‘app.get(‘/api/student/:id’)’ method looks up a single student by its id.

That’s it! You have a working web API. Run the app:

$ node app.js
Load http://localhost:3000/student in a browser to see the output.

for single user

Load http://localhost:3000/student/1 in a browser to see the output.