We want to create a form that will be able to send text to a server we'll also implement. You need to open with Live Server the client in order to make this work.
Resources:
- NodeJS URL module - Link
- NodeJS Http module - Link
- File System module - Link
- Check if file exists in NodeJS - Link
Video describing this exercise (Spanish) - Link
Create a server in your local machine that will be listening to incoming requests in port 3000. Submit the form for the first time with any information. Is the server receiving all the fields from the form?
The server should always work if the incoming request is to the endpoint /sendtext and only if the client is performing a GET request. In any other case, it should respond back a 404 error with a messsage of your choice.
To pass this requirmenet, just try to console.log que content parameter that you receive from the client GET request.
The server should write information to a file everytime it receives a petition.
- If the filename.txt written by the user does not exists; the server will create a new file with the information coming from the content field
- If the filename.txt already exists, the new information coming from content should be appended at the end of the file.
The filename can be stored in the same folder that app.js.
Submit a new form entry with this infromation: Filename: duckduckgo Content: this is just a test.
Now, try to navigate to http://localhost:3000/duckduckgo.txt. Can you see the file? Why or why not?
Open a new Git Bash terminal in your server project folder
-
Write a command to show all the files with TXT extension sorted by modification date.
-
Write another command to watch every second the files sorted by modification date. Perform a new submit in the form and notice if the file list is updated
-
Store your files in path named files; instead of the root directory of the server.
-
Use the Path module to create the path to the filename you want to store, insted of concateneiting strings.
-
Introduce some client validation to avoid the user to input more that 1024 caracters in the contect field form. Add also some server validation: if the server receives a content with more than 1024, it should response an 500 error; with a message informing the client that the text submitted is too long.