Node.js

Create a Simple Express Server

Now, let's write the basic structure of our server. Open your server.js file and add the following code:

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

require("dotenv").config();

const app = express();

// Middleware setup
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());

const PORT = process.env.PORT || 4000;

// Sample endpoint
app.get('/', (req, res) => {
  res.send('Vonage Verify backend is running.');
});

// Start server
app.listen(PORT, () => {
  console.log(`Server is listening on port ${PORT}`);
});
  • We import Express and CORS for building the API.
  • We use express.json() and express.urlencoded() to parse incoming request bodies.
  • The cors() middleware allows requests from any origin.
  • We add a simple endpoint (/) that returns a message to confirm that the server is running.

Time to test our backend implementation. Run the server:

node server.js

And visit http://localhost:3000 in your browser. You should see:

Vonage Verify Backend is running

If you see this, congratulantions! your server is working correctly!