Skip to content

Commit

Permalink
added env file
Browse files Browse the repository at this point in the history
  • Loading branch information
vasu962 committed Mar 2, 2024
1 parent 6baccdf commit 413fc3a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 119 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import express from "express";
import bodyParser from "body-parser";
import dotenv from 'dotenv';

dotenv.config();

const app = express();
const port = 4000;
const port = process.env.PORT || 3000;


// In-memory data store
let posts = [
Expand Down
11 changes: 11 additions & 0 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"axios": "^1.4.0",
"body-parser": "^1.20.2",
"dotenv": "^16.4.5",
"ejs": "^3.1.9",
"express": "^4.18.2"
}
Expand Down
55 changes: 29 additions & 26 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,82 @@
import express from "express";
import bodyParser from "body-parser";
import axios from "axios";
import express from 'express';
import bodyParser from 'body-parser';
import axios from 'axios';
import dotenv from 'dotenv';

dotenv.config();

const app = express();
const port = 3000;
const API_URL = "http://localhost:4000";
const port = process.env.PORT || 3000;
const API_URL = process.env.API_URL || 'http://localhost:4000';

app.use(express.static("public"));
app.use(express.static('public'));

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// Route to render the main page
app.get("/", async (req, res) => {
app.get('/', async (req, res) => {
try {
const response = await axios.get(`${API_URL}/posts`);
console.log(response);
res.render("index.ejs", { posts: response.data });
res.render('index.ejs', { posts: response.data });
} catch (error) {
res.status(500).json({ message: "Error fetching posts" });
res.status(500).json({ message: 'Error fetching posts' });
}
});

// Route to render the edit page
app.get("/new", (req, res) => {
res.render("modify.ejs", { heading: "New Post", submit: "Create Post" });
app.get('/new', (req, res) => {
res.render('modify.ejs', { heading: 'New Post', submit: 'Create Post' });
});

app.get("/edit/:id", async (req, res) => {
app.get('/edit/:id', async (req, res) => {
try {
const response = await axios.get(`${API_URL}/posts/${req.params.id}`);
console.log(response.data);
res.render("modify.ejs", {
heading: "Edit Post",
submit: "Update Post",
res.render('modify.ejs', {
heading: 'Edit Post',
submit: 'Update Post',
post: response.data,
});
} catch (error) {
res.status(500).json({ message: "Error fetching post" });
res.status(500).json({ message: 'Error fetching post' });
}
});

// Create a new post
app.post("/api/posts", async (req, res) => {
app.post('/api/posts', async (req, res) => {
try {
const response = await axios.post(`${API_URL}/posts`, req.body);
console.log(response.data);
res.redirect("/");
res.redirect('/');
} catch (error) {
res.status(500).json({ message: "Error creating post" });
res.status(500).json({ message: 'Error creating post' });
}
});

// Partially update a post
app.post("/api/posts/:id", async (req, res) => {
console.log("called");
app.post('/api/posts/:id', async (req, res) => {
console.log('called');
try {
const response = await axios.patch(
`${API_URL}/posts/${req.params.id}`,
req.body
);
console.log(response.data);
res.redirect("/");
res.redirect('/');
} catch (error) {
res.status(500).json({ message: "Error updating post" });
res.status(500).json({ message: 'Error updating post' });
}
});

// Delete a post
app.get("/api/posts/delete/:id", async (req, res) => {
app.get('/api/posts/delete/:id', async (req, res) => {
try {
await axios.delete(`${API_URL}/posts/${req.params.id}`);
res.redirect("/");
res.redirect('/');
} catch (error) {
res.status(500).json({ message: "Error deleting post" });
res.status(500).json({ message: 'Error deleting post' });
}
});

Expand Down
92 changes: 0 additions & 92 deletions solution.js

This file was deleted.

0 comments on commit 413fc3a

Please sign in to comment.