DEV Community

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

Posted on • Updated on

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! 😃

Top comments (0)