Skip to content

Commit

Permalink
adds render example for NP API
Browse files Browse the repository at this point in the history
  • Loading branch information
rbabyuk-vs committed Apr 22, 2024
1 parent fc6dbbb commit 2bb9d6b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
36 changes: 36 additions & 0 deletions nodejs-np-api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const express = require('express');
const app = express();
const path = require('path');
const fetch = require('node-fetch');

// Маршрут для проксування запитів до Nova Poshta API
app.post('/api/nova-poshta', async (req, res) => {
const requestBody = req.body;
const apiUrl = 'https://api.novaposhta.ua/v2.0/json/';
const apiKey = process.env.NOVA_POSHTA_API_KEY;

try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'apiKey': apiKey
},
body: JSON.stringify(requestBody)
});

const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'An error occurred' });
}
});

// Обслуговування статичних файлів з папки клієнтської частини
app.use(express.static(path.join(__dirname, 'client')));

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
8 changes: 8 additions & 0 deletions render.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
- type: web
name: nova-poshta-api-demo
env: node
plan: free
buildCommand: npm install
startCommand: npm start
envFile: .env

0 comments on commit 2bb9d6b

Please sign in to comment.