Here is the code for the index.js of my expressjs project. I'm new to Node.js and trying to understand how to develop a web app using MERN Stack.
import express from "express";
import bodyParser from "body-parser";
import pg from "pg";
const app = express();
const port = 3000;
const db = new pg.Client({
user: "postgres",
host: "localhost",
database: "world",
password: "12345678",
port: 5432,
});
db.connect();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
async function checkVisisted() {
const result = await db.query("SELECT country_code FROM visited_countries");
let countries = [];
result.rows.forEach((country) => {
countries.push(country.country_code);
});
return countries;
}
// GET home page
app.get("/", async (req, res) => {
const countries = await checkVisisted();
res.render("index.ejs", { countries: countries, total: countries.length });
});
//INSERT new country
app.post("/add", async (req, res) => {
const input = req.body["country"];
const result = await db.query(
"SELECT country_code FROM countries WHERE country_name = $1",
[input]
);
if (result.rows.length !== 0) {
const data = result.rows[0];
const countryCode = data.country_code;
const final_data = await db.query("INSERT INTO visited_countries (country_code) VALUES ($1)", [
countryCode,
]);
res.redirect("/");
}
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});`
When trying to reach http://localhost:3000/add route, I get this error:
