O'Reilly logo

Write Modern Web Apps with the MEAN Stack: Mongo, Express, AngularJS, and Node.js by Jeff Dickey

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required