zkt25/assignment2/server.js

36 lines
798 B
JavaScript

const express = require('express');
const bodyParser = require('body-parser');
const { Pool } = require('pg');
const app = express();
const port = 3000;
app.use(bodyParser.json());
const pool = new Pool({
host: 'postgres',
user: 'weatheruser',
password: 'weatherpass',
database: 'weatherdb',
port: 5432
});
app.post('/log', async (req, res) => {
const { city, temperature, description } = req.body;
try {
await pool.query(
'INSERT INTO weather_log (city, temperature, description) VALUES ($1, $2, $3)',
[city, temperature, description]
);
res.status(200).send('Logged!');
} catch (err) {
console.error(err);
res.status(500).send('Error saving data');
}
});
app.listen(port, () => {
console.log(`Weather API listening on port ${port}`);
});