OthDr / app-express

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

News_Express Application

Express.JS REST API for news

How to use:

  • npm install to install all node-modules.
  • npm start start server.
  • npm test run test.

HTTP : GET POST PATCH DELETE

  • JSON request & response.
  • URlemncoded(x-www-form-urlencoded) requests.

Database Mongodb : with mongoose

  • Collection 1: for news articles
  • Collection 2: for news authors

POST request body validation & Errors

using Joi object schema const Joi = require('joi')

  • Used Joi package : Created an object schema to validate the req.body.
  • Used mongoose schema : mongoose ORM for Mongodb provided schema validation of db models.
  • Send back the error on the response.

Middleware

body-parser const bParser = require('body-parser'): To parse request body JSON or urlencoded .

routes: response can be redered on HTML page

GET :

  • GET('/news') recent news

  • GET('/news/country=:c_code') news based on country code

  • GET('/news/category=:cat_code') news based on category

  • GET('/news/:id') get a post by it's ID

POST :

  • POST('/add_news') create a new post

PATCH :

  • PATCH('/add_news') update an existing post in the database

DELETE :

  • DELETE('/add_news') delete a post from the databse

testing

Mocha & chai

"scripts": { "test": "mocha --exit" }
chai-http (request testing), should & expect (Assertions)

About news:

  • News countries: countries = ['dz','us','fr','ru','in','de','tr','eg','br','ca','cz','it','ua']
  • News categories: categories =['business','entertaiment','general','health','science','sports','technologie']

example URL :

"http://localhost:3000/api/news/category=sports"
"http://localhost:3000/api/news/country=dz"

About


Languages

Language:JavaScript 71.6%Language:CSS 16.0%Language:EJS 12.4%