Newby Coder header banner

NodeJs Basic Authentication Server

Provided example uses a Nodejs Express server to expose a Rest API

Express is a Nodejs web application framework

It is used to assign routes (or paths) that are to be exposed by a server and add middlewares

Check Nodejs Express server for brief info

Creating new Node.js Application

Create a new directory

mkdir rest_server

The name of a directory should not match that of any (nodejs) dependency to be used

Change directory to newly created directory

cd rest_server

Run npm init command to create a package.json file so that node packages can be installed for current package

npm init -y

-y is added so that it doesn't prompt for setting some common info about package


Run following command from project directory

npm install express --save


index.js - Server entry point

Save following code in index.js inside package directory

// import express
const express = require('express')
const app = express()
const port = 3000

app.get('/', (request, response) => {
  response.send('Message from Express server')

// Set a route '/numbers' which accepts Get requests
app.get('/numbers', function(req, res, next) {
  // Use json() method of res (response) to return a json
  res.json({'serverData' : [
    { id: 11, name: 'eleven' },
    { id: 12, name: 'twelve' },
    { id: 13, name: 'thirteen' },
    { id: 14, name: 'fourteen' },
    { id: 15, name: 'fifteen' },
    { id: 16, name: 'sixteen' },
    { id: 17, name: 'seventeen' },
    { id: 18, name: 'eighteen' },
    { id: 19, name: 'nineteen' },
    { id: 20, name: 'twenty' },

//start server
app.listen(port, (err) => {
  if (err) {
    return console.log('error', err)
  console.log(`server is listening on port: ${port}`)

Run instructions

Run following command from project directory

nodejs index.js

To make it as accessible over a local network such as wifi, enter local ip address with command

nodejs index.js

Here, is ip address


Enter url in a browser to test the Apis such as


Mobile Applications

Following mobile applications call rest Api

Ensure that mobile application code calls the ipaddress or dns name that the server is hosted in

Check connecting React native app to basic auth server or connecting Flutter app