This is PRE-Express. Once we get to Express, this will not be steps needed to build apps. You will obviously need Node. A very useful addition would be to download Nodemon. This will make it so you will not need to turn off and run your server again each time you make a change that you would like to see.
First you will need to initialize/create the application. Create the directory where you would like this to live. Once you are in your new directory:
- Run ‘npm init’
- Change Entry Point to "app.js"
- Enter till asked if correct (yes)
- Touch "app.js"
- code . to open your IDE
Ok, so now you have the app started and your main JS file in there. Go into that app.js file and paste in:
const http = require('http')
const port = 3000
const server = http.createServer(function(request, response){
response.end ("hi class")
})
server.listen(port)
console.log(`Server is now up and running @ port: ${port}`)
This should give you what you need to have a sever and run it on port 3000 in your browser.
- Run ‘node app.js’1
- http://localhost:3000/
- You should see a page with "hi class"
- Create new repo with app name
- Do not add readme or anything.
- git init
- git add .
- git commit -m "first commit"
- git remote add origin https://github.com/'USERNAME'/'repo-name'.git 2
- 'git push' will throw an error
- 'git push origin master' will push to your repo only once. You need to set the upstream to continually push.
- git push --set-upstream origin master
if (request.url === "/" && request.method === "GET") {
// response.end("Check back later for games details")
fs.readFile("text.txt", function (error, data) {
if (error) {
response.writeHead(400)
return response.end(`${error}`);
} else {
response.writeHead(200, { "Content-type": "text/html" });
response.write(data);
return response.end();
}
});
}
- Create if statement with the 'request.url'. Adding the 'request.method' in the if statement makes it stronger.
- Inside the if statement, use node file share. 'fs.action' This could be readFile,writeFile,appendFile,3 etc.you will need to designate a path or specific file and declare a callback function with error & data in that order.
- Create another if statement to declare error. The writeHead should be 400 and return the error message.
- Create an else statement to return the data.
- 200 - OK
- 400 - Bad Request
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
- Create
- POST or add new entries
- Read
- GET retrieve, search, or view existing entries
- Update
- PUT or edit existing entries
- Delete
- DELETE deactivate, or remove existing entries
filled out soon