Git Product home page Git Product logo

expiryapp's Introduction

ExpiryApp

This is a study/learn tutorial that runs on MEAN(Mongo ExpressJS Angular NodeJS) stack.

Install dependencies

npm install This will install the project dependencies

Build

Run ng build to build the project. The build artifacts will be stored in the dist/ directory. Use the --prod flag for a production build.

Prepare

Make sure NodeJS is installed, MongoDB installed and running(use mongodb to run) and has the required data.

Running the project

Run node server and open http://localhost:3001 in a browser. Here we go.

Server code(server.js)

api file for interacting with MongoDB

const api = require('./server/routes/api');

parsers

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

angular/front end code output

app.use(express.static(path.join(__dirname, 'dist/expiryApp')));

api location

app.use('/api', api);

send requests to angular app

api.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'dist/expiryApp/index.html'));
});

set port

const port = process.env.PORT || '3001';
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => console.log(`Running on localhost: ${port}`));

API code(server/routes/api.js)

establishing connection

const connection = (closure) => {
  return MongoClient.connect('mongodb://localhost:27017/products', (err, db) => {
    if (err) return console.log(err);

    closure(db);
  });
};

error case handler

const sendError = (err, res) => {
  response.status = 501;
  response.message = typeof err === 'object' ? err.message : err;
  res.status(501).json(response);
};

success case handler

const response = {
  status: 200,
  data: [],
  message: null
};

'/vegitables' api

router.get('/vegitables', (req, res) => {
  connection((db) => {
    db.collection('vegitables')
    .find()
    .toArray()
    .then((users) => {
      response.data = users;
      res.json(response);
    })
    .catch((err) => sendError(err, res));
  });
});

'/fruits' api

router.get('/fruits', (req, res) => {
  connection((db) => {
    db.collection('fruits')
    .find()
    .toArray()
    .then((users) => {
      response.data = users;
      res.json(response);
    })
    .catch((err) => sendError(err, res));
  });
});

'/groceries' api

router.get('/groceries', (req, res) => {
  connection((db) => {
    db.collection('groceries')
    .find()
    .toArray()
    .then((users) => {
      response.data = users;
      res.json(response);
    })
    .catch((err) => sendError(err, res));
  });
});

expiryapp's People

Contributors

johnsackson avatar sacksonjohn avatar

Watchers

 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.