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! πŸ˜ƒ

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Image of Docusign

πŸ› οΈ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more