DEV Community

Cover image for Using DEV.to API and Node.js
Lorna Watson
Lorna Watson

Posted on • Edited on

1 1

Using DEV.to API and Node.js

On my website I originally implemented a link back to here, my blog, in the nav bar. I've been recently playing around a lot with Node.js and APIs. My aim was to display basic post info such as:

  • Title
  • Description
  • Tags
  • URL
  • Created date

The end result was this: (site isn't currently reflecting this change and isn't finished)

Alt Text

Let's begin with the usual setup, create a new project folder in your workplace and cd init. Run npm init and install the following packages:

var express = require('express'),
    cors = require("cors"),
    app = express(),
    bodyParser = require('body-parser'), 
    axios = require('axios');
Enter fullscreen mode Exit fullscreen mode

The code is pretty straightforward, a simple GET so just need to call http://localhost:3000/api/posts to get the JSON response data.

var express = require('express'),
    cors = require("cors"),
    app = express(),
    bodyParser = require('body-parser'), 
    axios = require('axios');

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

app.use(cors());

app.get("/api/posts", (req, res) => { 
    axios.get('https://dev.to/api/articles?username=lornasw93').then(resp => {
        res.send(resp.data);
    }).catch(err => {
        res.send(err);
    });
});

const PORT = process.env.PORT || 3000;
module.exports = app.listen(PORT, () => {
    console.log('Server running on port %d', PORT);
})
Enter fullscreen mode Exit fullscreen mode

The repo lives here.

This project is very much a work in progress so will update the repo when needed.

Thanks! 😃

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay