I am stuck and cant seem to get this to work#7
I am stuck and cant seem to get this to work#7Seraphineyoung wants to merge 1 commit intoCodeYourFuture:masterfrom Seraphineyoung:master
Conversation
| console.log("Server is loading a student page"); | ||
| res.send("hello student"); | ||
|
|
||
| app.post("/contact.html", function(req, res) { |
There was a problem hiding this comment.
app.post(...) tells you application that when a post request to "/contact.html" happens, execute the code within. This app.post is within an app.get. This means that the post route is never registered with the app, and when you do the get request, you then try to register a new route. I imagine this would break the server.
Make sure you do all your app.get and app.post on the same level. Read through this for examples: https://expressjs.com/en/guide/routing.html
| fs.writeFile("data/posts.json", req.fields, function(error) { | ||
| res.send("Thank your for filling this form. cooool"); | ||
|
|
||
| fs.readFile(__dirname + "data/posts.json", function(error, content) { |
There was a problem hiding this comment.
Trying to do multiple reads and writes within callbacks is never a good idea! :) Each get or post should only be doing a single thing. Try to break fit what you are trying to do into a get or post request, and then only do that within the request.
|
|
||
| var newContent = JSON.stringify(parsedFile); | ||
| //append data to file | ||
| fs.appendFile("data/posts.json", newContent, function(err) { |
There was a problem hiding this comment.
A post should only be appending data. There is no need to write the file, then read it, then append it. Try to keep things as simple as possible :)
I have been on this task for a couple of days now and still can figure how to append the data. Instead of my server to work it has completely broken down.