This repository was archived by the owner on Aug 17, 2024. It is now read-only.
forked from node-girls/express-workshop
-
-
Notifications
You must be signed in to change notification settings - Fork 93
Add solution to part one of the express workshop exercise #3
Open
rajgthub
wants to merge
4
commits into
CodeYourFuture:master
Choose a base branch
from
rajgthub:raj/express_workshop_solutions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1 @@ | ||
| { | ||
| "1467390356291": "This is my very first blog post!" | ||
| } | ||
| [{"blogpost":"first post"},{"blogpost":"second post"},{"blogpost":"third post"}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "name": "express-workshop-forked", | ||
| "version": "1.0.0", | ||
| "description": "this is to practice basic node and express", | ||
| "main": "server.js", | ||
| "repository": "git@github.com:rajgthub/express-workshop.git", | ||
| "author": "raj <rraju12@gmail.com>", | ||
| "license": "MIT", | ||
| "scripts":{ | ||
| "dev": "nodemon server.js -e js,hbs" | ||
| }, | ||
| "dependencies": { | ||
| "express": "^4.16.3", | ||
| "express-formidable": "^1.0.0", | ||
| "hbs": "^4.0.1" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| module.exports = ( app) => { | ||
| app.get('/darshboard', (req, res) => { | ||
| res.render('darshboard.hbs', { | ||
| pageTitle: "Darshboard page", | ||
| content: "Welcome to darshboad page", | ||
| }) | ||
| }) | ||
| app.get("/darshboard/contact", (req, res) => { | ||
| res.render("contact.hbs", { | ||
| pageTitle: "Contact page", | ||
| }); | ||
| }); | ||
| app.get("/darshboard/about", (req, res) => { | ||
| res.render("about.hbs", { | ||
| pageTitle: "About page", | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| //core modules | ||
| const fs = require('fs') | ||
|
|
||
| //3rd party modules | ||
| const express = require('express') | ||
| const formidable = require("express-formidable"); | ||
| const hbs = require('hbs') | ||
| //variables | ||
| const port = 3000 | ||
| const pathToPostsFile = __dirname + "/data/posts.json"; | ||
|
|
||
| //create app | ||
| const app = express() | ||
|
|
||
|
|
||
| //serving public assets middleware | ||
| app.use(express.static('public')) | ||
| hbs.registerPartials(__dirname + '/views/partials') | ||
| app.set('view engine', 'hbs') | ||
| hbs.registerHelper("getCurrentYear", () => new Date().getFullYear()); | ||
| hbs.registerHelper("setUpper", text => text.toUpperCase()); | ||
| app.use(formidable()); | ||
|
|
||
| // router | ||
| app.get('/posts', (req, res) => { | ||
| res.sendFile(pathToPostsFile); | ||
| }) | ||
| app.post('/create-post', (req, res) => { | ||
| //reading posts | ||
| let allposts = JSON.parse(fs.readFileSync(pathToPostsFile), 'utf8'); | ||
| const post = req.fields; | ||
| allposts.push(post) | ||
| fs.writeFileSync(pathToPostsFile, JSON.stringify(allposts)); | ||
| res.sendFile(pathToPostsFile); | ||
| }) | ||
| require('./router/routes')(app) //routes for handlebars | ||
| app.listen(port, (req, res) => { | ||
| console.log("Server is runnig on port:", port); | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
|
|
||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <style> | ||
| .container { | ||
| margin: auto 25%; | ||
| } | ||
| </style> | ||
| <title>Handlebars example</title> | ||
| </head> | ||
|
|
||
| <body> | ||
| <section class="container"> | ||
| {{> header}} | ||
| <nav> | ||
| <a href="/darshboard">Home</a> | ||
| </nav> | ||
| <main>{{setUpper "Welcome to about page"}}</main> | ||
| {{> footer}} | ||
| </section> | ||
|
|
||
| </body> | ||
|
|
||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
|
|
||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <style> | ||
| .container{ | ||
| margin: auto 25%; | ||
| } | ||
| </style> | ||
| <title>Handlebars example</title> | ||
| </head> | ||
|
|
||
| <body> | ||
| <section class="container"> | ||
| {{> header}} | ||
| <nav> | ||
| <a href="/darshboard">Home</a> | ||
| </nav> | ||
| <main>{{setUpper "Welcome to contact page"}}</main> | ||
| {{> footer}} | ||
| </section> | ||
|
|
||
| </body> | ||
|
|
||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
|
|
||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <style> | ||
| .container { | ||
| margin: auto 25%; | ||
| } | ||
| </style> | ||
| <title>Handlebars example</title> | ||
| </head> | ||
|
|
||
| <body> | ||
| <section class="container"> | ||
| {{> header}} | ||
| <nav> | ||
| <a href="/darshboard/contact">Contact page</a> | ||
| <a href="/darshboard/about">About page</a> | ||
| </nav> | ||
| <main>{{setUpper "Welcome to darshboar page"}}</main> | ||
| {{> footer}} | ||
| </section> | ||
|
|
||
| </body> | ||
|
|
||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <footer> | ||
| <h3>Created by Raj - © All rights reserved - {{getCurrentYear}}</h3> | ||
| </footer> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <header> | ||
| <h1>{{pageTitle}}</h1> | ||
| </header> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try to use
constorletinstead ofvar, asvarofter behaves in mysterious waysThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I will do it. I did not realise as it was in the forked version of the code (original).