> For the complete documentation index, see [llms.txt](https://kazuhide-tony-s-organization.gitbook.io/intro-to-chatgpt-chatbot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kazuhide-tony-s-organization.gitbook.io/intro-to-chatgpt-chatbot/getting-started/editor.md).

# 3. Introduction to Web Backend Using JavaScript

### Setting Up a Simple Node.js Server

1. **Initialize a Node.js Project**:

   ```bash
   npm init -y
   ```
2. **Install Express**:

   ```bash
   npm install express
   ```
3. **Create a Simple Server**:

   * Create `server.js`:

   ```javascript
   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:

   ```bash
   node server.js
   ```

**Connecting Backend with GenAI API**

1. **Install Axios**:

   ```bash
   npm install axios
   ```
2. **Create an Endpoint to Call GenAI**:

   * Update `server.js`:

   ```javascript
   const axios = require('axios');

   app.post('/chat', async (req, res) => {
     const response = await axios.post('https://api.openai.com/v1/chat/completions', {
       model: 'gpt-3.5-turbo',
       messages: [{ role: 'user', content: req.body.message }],
       temperature: 0.7
     }, {
       headers: {
         'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
         'Content-Type': 'application/json'
       }
     });

     res.json(response.data);
   });
   ```
3. **Testing the Endpoint**:
   * Use a tool like Postman to send a `POST` request to `http://localhost:5000/chat`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kazuhide-tony-s-organization.gitbook.io/intro-to-chatgpt-chatbot/getting-started/editor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
