loading...
Cover image for I built a simple rest API with node and a Raspberry Pi

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

teamallnighter profile image Chris Connelly ・1 min read

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
sudo npm init -y
sudo npm install express
touch index.js && sudo nano index.js

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);

control x to save and quit

from the root

node index.js

now from postman

`http://your-server-ip:3000/api` 

You will see

API is online

Posted on by:

teamallnighter profile

Chris Connelly

@teamallnighter

My name is Chris. I love webdev so much I quit my well paying job of fifteen years to go back to school and start my own freelance compant

Discussion

markdown guide