3. Introduction to Web Backend Using JavaScript

Setting Up a Simple Node.js Server

  1. Initialize a Node.js Project:

    npm init -y
  2. Install Express:

    npm install express
  3. Create a Simple Server:

    • Create server.js:

    const express = require('express');
    const app = express();
    const port = 5000;
    
    app.get('/', (req, res) => {
      res.send('Hello from the backend!');
    });
    
    app.listen(port, () => {
      console.log(`Server running at http://localhost:${port}`);
    });
    • Run the server:

    node server.js

Connecting Backend with GenAI API

  1. Install Axios:

  2. Create an Endpoint to Call GenAI:

    • Update server.js:

  3. Testing the Endpoint:

    • Use a tool like Postman to send a POST request to http://localhost:5000/chat.

Last updated