Chapter 4. Building a Node.js API

In the previous chapter, you built a fully functioning Angular app for posting status updates. In this chapter, you will build an API for it to get a list of all the posts and to make a new post. The endpoints will be as follows:

GET /api/posts returns a JSON array of all the posts. This is similar to what you had in $scope.posts.

POST /api/posts expects a JSON document containing a username and post body. It will save that post in MongoDB.

The Stock Endpoint

To start, you’ll use Node.js and Express to build a stock /api/posts endpoint. First, inside a new folder, create a package.json file like the following:

{  "name": "socialapp"}

The name can be anything you want, but try to ensure it doesn’t conflict ...

Get Write Modern Web Apps with the MEAN Stack: Mongo, Express, AngularJS, and Node.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.