From 2bb9d6b92bb094adc7fced0d548c15ea6f55cb40 Mon Sep 17 00:00:00 2001 From: Roman Babyuk Date: Mon, 22 Apr 2024 21:22:12 +0300 Subject: [PATCH] adds render example for NP API --- nodejs-np-api/server.js | 36 ++++++++++++++++++++++++++++++++++++ render.yaml | 8 ++++++++ 2 files changed, 44 insertions(+) create mode 100644 nodejs-np-api/server.js create mode 100644 render.yaml diff --git a/nodejs-np-api/server.js b/nodejs-np-api/server.js new file mode 100644 index 0000000..3cb5068 --- /dev/null +++ b/nodejs-np-api/server.js @@ -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}`); +}); \ No newline at end of file diff --git a/render.yaml b/render.yaml new file mode 100644 index 0000000..86f2e47 --- /dev/null +++ b/render.yaml @@ -0,0 +1,8 @@ +services: + - type: web + name: nova-poshta-api-demo + env: node + plan: free + buildCommand: npm install + startCommand: npm start + envFile: .env \ No newline at end of file