Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.

Conversation

@Seraphineyoung
Copy link

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.

console.log("Server is loading a student page");
res.send("hello student");

app.post("/contact.html", function(req, res) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants