Write your first API in javascript with expressjs.

Write your first API in javascript with expressjs.

Requirements:

-basic understanding of web development and javascript

-install nodejs from here:- download

-any editor of your choice. i am using vscode.

fire up your editor and get ready to write your first api.

first init your nodejs project:

npm init

install expressjs:

 npm install express --save

install CORS:

npm install cors --save

-write some magical code and we are done!!, well not that easy but it is easy hang on.

you know about http requests right.

if not check out this article on MDN👇: http requests we are going to use fetch for api requests: using fetch

let's get our hands dirty and write some code:

server side code:

const express = require('express');
const app = express();
const cors = require('cors');
app.use(cors({
    origin: '*'
}));
app.get('/myroute', (req, res) =>{

  res.send(ack_msg);
  console.log(req.body);
});
app.listen(8002);

don't get confused here is explanation of above code:

-first let's import express library that we previously installed using require and using it as app instance.

const express = require('express');
const app = express();

-it's done.... yeah it is that simple

now we just need to use our freshly created app instance to route GET and POST requests.

GET requests are sent by client to get some data from server using fetch(in our case),

POST requests are sent by client to give some data to server like username , passwords, email etc. using fetch(in our case),

we will get into detail about fetch in our client side code.

let's use our app for receiving simple GET requests

app.get('/myroute', (req, res) =>{

  res.send(ack_msg);
  console.log(req.body);
});

now start a listener on your desired port

app.listen(8002);

-here we are accepting GET request from a web page on root route "/" you can add whatever route you want for example "/api" or "/myroute" you just need to add that in your requests like "localhost:8002/myroute".

-there are two basic things we need to handle. Incoming request data which is in req argument and sending response back to web page using "res".

-here we are sending text response "wow you created your first api" to web page you can you get fancy and can use json also.

-we are logging out body part of request using req.body in console.

we are also using cors to allow cross origin access.

-using cors is necessary as by default browser does allow request from other origins cors will help us bypass that. -here we are using "" which indicate the browser that request from all origins are allowed you can also whitelist using specific addresses inside " " certain origins not all but for sake of simplicity we are using "".

app.use(cors({
    origin: '*'
}));

-learn more about cors here: CORS

finally we are done:

-just start your server

node my_first_api.js

congratulation🎊, you've successfully wrote your first api in nodejs. now it's ready to be used.

let's use our api:

client side code:

just use fetch requests in your web page javascript file from where you want to get or post any data to your newly created api server.

fetch('http://localhost:8002/')
  .then(response => response.json())
  .then(data => console.log(data));

-here we are sending a get request to our api server using fetch as mentioned above. -fetch uses standard http request model to communicate to our api and server. -by default fetch uses get request until mentioned specifically for post. -In GET request we send request to server using our api route that we used "/myroute" and wait for response from server and that's what simple gist of api is it is used as communicating between different app efficiently and securely.

-OR you can also use Postman or Insomnia to send api request to your server also. Postman Insomnia i'm using simple browser you can use whatever you want it's your api.

Screenshot (153).png

congratulation🎊, you've successfully wrote and used your first api in nodejs. now you are ready to follow the journey of creating highly efficient and complex api routing for big projects or maybe for your next unicorn startup.

always learning

if you have any question comment down :

drop a like if it helped you in any way.

if you want to chat or discuss tech follow me on twitter:

twitter