Giter Club home page Giter Club logo

mongodbnodejswebapi's Introduction

MongoDb Node.js Web API

This is an example Web API created in Node.js that connects to MongoDb. You can get a new MongoDb cloud account here. Once you create an account, you can create a database and a collection, you can modify the settings in this file. As an alternative, you can also provision a MongoDb account for yourself on Azure. The MongoDb Node.js driver works just the same in both the cases, only difference is the difference between the cluster Urls.

Deployment

You can deploy the Web API on any server that can run the Node.js runtime. Alternatives that I know are IIS on a Windows server or workstation, Windows Azure and Aamazon Web Services. Node.js apps run equally well on Linux and Windows servers.

Further use

You can add many more services to the same Node.js app or create new apps for more services, each app per service to conform to micro-services architecture.

Architecture

The architecture is very simple. It has a starter .js called app.js file that composes a service object that actually connects to a MongoDb cluster. App.js exposes api endpoints.

Build

Following snippet shows how to connect to a MongoDb db from Node.js app (from here). Very similar code (except the url to the cluster) can be used for connecting to all kinds of provisions such as Azure, AWS etc.

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://kay:[email protected]/admin";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});

Simply run node app.js to debug and test the app locally. It will be available at the url specified via var port = process.env.PORT || 1337;. On cloud deployments, it's accessible from the base urls and api endpoints can be accessed by apending api/{op} to the base urls in both the local and cloud deployment. e.g. locally https://localhost:1337/api/getaccounts.

mongodbnodejswebapi's People

Contributors

prasadnarwadkar avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.