Backend API REST with NodeJS

Pablo Ezequiel
Feb 24, 2017 · 3 min read

Let´s create it from scratch

$ npm init

$ npm install mongoose — save
$ npm install express — save
$ npm install body-parser — save — save-dev

#01 Coding — snippet 01

#Code Snippet 02

Api Server with Mongo DB and the API REST for places implemented

We will invoke with the postman client: Let see that the content-type is application/json to send the json body to the server

We can observe that it is called with a “post” method

And it was inserted to the database:

#03 Insert into the database

Notas about insert

I will add two fields, x and y that don´t belong to the schema:

The result is that x and y are suppress in the insert process to the database:

How to use mongoose model without schema?

With strict in false, we can insert also x,y in the database:

Strict set to false

Some highlights about MongoDB:

a) Insert in MongoDB: you don´t need an Schema, but this is a good practice

b) Insert in MognoDB: you can add fields that are not defined in the schema

c) It is interesting to compare a SQL a non SQL table:

from: https://www.mongodb.com/compare/mongodb-mysql

we read:

  • Table vs Collection
  • Row vs Document
  • Column vs Field
  • Joins vs Embedded Documents, links (About this point there is a good practice that is link document in the less cardinality direction…)

And the Query Language differences

Other interesting tips about mongoose:

Virtuals: are document properties that you can get and set but that do not get persisted to MongoDB. The getters are useful for formatting or combining fields.

GeoJSON: This is a standard to represent geographic data structures, like lineal, point, etc. MongoDB no only support save this kind of points, but also do specific queries of “near”, “distance”, etc. (geospatial queries: queries for inclusion, intersection and proximity: more info here or may be here)

If I have a little time, I will add some of it to the api.

I will stop the post at this stage, I guess that soon I will add the GeoJSON example

Pablo Ezequiel

Written by

Dev and Tech. Sharing while I am learning (#cloud, #mobile)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade