Create a News app API using Node.js

Rudraksh Dixit
3 min readMar 31, 2020

To interact with the other systems Application Program Interface(API) is needed. Your mobile application will request the server for data and the server sends back the data if its present.

In this article, I will show you how to create your own simple Restful API for a News application.

Check out a flutter application of News using this API

Firstly open the terminal and create a new directory, then go to the newly created directory and write a command npm init

Fill out all the details or skip it by pressing enter. Then install some npm packages which will be used in the project.

npm i express mongoose body-parser

Now create a new file named app.js in the project directory. Open app.js in your favourite code editor.

//jshint esversion:6const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require('mongoose');
const app = express();app.use(bodyParser.urlencoded({ extended: true })); app.listen(process.env.PORT||3000,function(){
console.log("server is started on port 3000");
});

This is the basic structure for the express app, which is running on port 3000 locally. You can check the status by writing a command node app.js in your terminal and then going to the link http://localhost:3000/ in your browser.

Now initializing the MongoDB for our project, to learn how to create your own MongoDB cluster visit here.

mongoose.connect('mongodb+srv://rd:newsapp@cluster0-zbbtc.mongodb.net/newsDB', {useNewUrlParser: true, useUnifiedTopology: true });

Now creating a schema for our project i.e how the data will be stored in our No SQL database, data will be stored in key-value pair. Here the title is news title, content is the body of the news, imageURL is the link where is image is present and the author is the one who wrote the news.

const newsSchema={
title: String,
content: String,
imageURL: String,
author: String,
};

Creating a mongoose model from our Schema

const News=mongoose.model("News",newsSchema);

So its time to create routes where the users can call and get the data for your database.

Creating a route “/” and getting all the data present. It will send all the data present in our database and if something goes wrong, an error message will be sent.

app.get("/",function(req,res){
News.find(function(error,news){
if(!error){
res.send(news);
}else{
res.send("error");
}
});
});

Now create a post request from where the admin can post the news articles. Since its a basic level API so we aren’t concerned about the security and authentication.

Creating a post route at “/featured”

app.post("/featured",function(req,res){const newNews= new News({
title: req.body.title,
content: req.body.content,
imageURL: req.body.imageURL,
author: req.body.author,
});
newNews.save(function(err,news){
if(err){
res.send(err);
}else{
res.send("Added Succesfully");
}
});
});

What if a user wants to search a specific news article, using regex we can send them the results based on the news title and the author name.

Creating a route “/:newsTitle” where the search operation will be performed.

app.get("/:newsTitle",function(req,res){
const news=req.params.newsTitle;
News.find({$or:[
{title:{$regex: new RegExp(news, "ig")} },//based on title
{author:{$regex: new RegExp(news, "ig")} }//based on author
]},function(err,founditems){
if(founditems){
res.send(founditems);
}
else{
res.send("error");
}
});
});

So here is our simple API ready to be published. Publish your node API on Heroku for free.

Open Postman and make some post request on the “/featured” route to publish an article.

To see the whole source code visit here.

You can also email/message me for further queries.

Email: Rudrakshdixit@gmail.com

Facebook: http://bit.ly/2ELzDcL

Twitter: https://bit.ly/2JqWOdZ

Linkedin: https://bit.ly/2w4aCrO

--

--