DEV Community

Cover image for I built a simple rest API with node and a Raspberry Pi
Chris Connelly
Chris Connelly

Posted on

I built a simple rest API with node and a Raspberry Pi

This was a lot of fun for me and I will be updating this more as I go!

I installed ubuntu server on a raspberry pi 4.

From var/www/html

mkdir api && cd api
Enter fullscreen mode Exit fullscreen mode
sudo npm init -y
Enter fullscreen mode Exit fullscreen mode
sudo npm install express
Enter fullscreen mode Exit fullscreen mode
touch index.js && sudo nano index.js
Enter fullscreen mode Exit fullscreen mode

For now I am going to just make one GET response to test.

var express = require('express');        
var app = express();                 
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;  
const router = express.Router();
router.get('/', function(req, res) {
    res.json({ message: 'API is Online!' });   
});

app.use('/api', router);
app.listen(port);
console.log('Listening on port ' + port);
Enter fullscreen mode Exit fullscreen mode

control x to save and quit

from the root

node index.js
Enter fullscreen mode Exit fullscreen mode

now from postman

`http://your-server-ip:3000/api` 
Enter fullscreen mode Exit fullscreen mode

You will see

API is online
Enter fullscreen mode Exit fullscreen mode

Top comments (0)