Save and Delete Content in Express with HTTP Verbs

Share this video with your friends

Send Tweet

In this lesson you will learn how to support data modification by handling HTTP verbs besides GET. We will look at how to edit, save, and delete content with .put and .delete.

Martin
Martin
~ 9 years ago

I'm sure if it's deliberate, but it's like you skipping over the part surrounding the getUser function. I seems straightforward, but there's no real description to the change from the list to the actual user files. At least not what I can see.

Ben Clinkinbeard
Ben Clinkinbeard(instructor)
~ 9 years ago

Woah, you're right. Sorry about that!

Vishwas Chouhan
Vishwas Chouhan
~ 9 years ago

The tutorials going great but missing out on the utility functions and how they handling data throws me off and I can't follow as I am trying to build the app following the video.

Stefan
Stefan
~ 8 years ago

Hey, i found one problem if i start my server with "npm dev" from your previous lectures, not load a user picture because server restarting and not return picture. Do you have a solution about it ?

Ben Clinkinbeard
Ben Clinkinbeard(instructor)
~ 8 years ago

Hi Stefan,

I'm not seeing that issue.What action in the app is causing the server to restart? If it's editing a user, one thing you could try is changing the dev script to tell nodemon to ignore the users directory. nodemon index.js --ignore users/.

Hope that helps, Ben

Stefan
Stefan
~ 8 years ago

Thanks for your answer, now this working good.

Jaclyn
Jaclyn
~ 8 years ago

I agree! I like the project & am learning how routing works but there are some roadblocks that might not be necessary for the learning to keep moving forward.

The comment was 7 months ago so I'll just reiterate... I am constantly having to go to the github repo to see what functions were being built or refactored. It's a good practice in reading the code and seeing what's happening when but a detriment in following the video. WRiting the code with you in the video is not sufficient because there are gaps in what is being built and what is shown. A short heads up on 'so I built this getUser function to fix this problem' would be super helpful. Same thing with the users photos - I didn't clone the repo for this lesson down because I wanted to start from scratch to learn so it would be helpful to have a heads up and say, 'hey, get a collection of photos that you want to use so you can learn to display photos with your user'

Joel
Joel
~ 8 years ago

The packaged version of nodemon in the repo only watches js by default, but I ran a more recent nodemon (because didn't clone the repo) where nodemon watches .json as well by default, so I had the same issue as Stefan reported of nodemon restarting when .json files where modified or removed.

The workaround nodemon index.js --ignore users/ didn't work for me though, nodemon seems to want the --watch flag to watch a file, so: nodemon --watch index.js. Or nodemon --ignore users/ or nodemon --ignore .json would work as well.

George Katsanos
George Katsanos
~ 7 years ago

Exactly what Jaclyn said. I admit I am rather disappointed as the cost of the yearly subscription is rather high compared to other online courses (codeschool) which are much more paced and allow following the course without having to pause/copy code from github/resume.. For example, where's the explanation about require('path') ? .. Any mention about the saveUser() function? If I wanted to copy/paste code I wouldn't buy an online video learning subscription. :(

~ 7 years ago

Agreed. Steps being skipped over is very irritating.

Benjamin
Benjamin
~ 7 years ago

how are we able to use jquery in the handlebars template? i don't remember ever adding it?

Anett_Soos
Anett_Soos
~ 6 years ago

agreed! skipping steps is not cool, I completely lost what is going on. I wish it was addressed and more videos put in as I just give up at this point.

Cuong
Cuong
~ 6 years ago

Yes there are some steps being skipped in the video. I am trying to build as a watch the video and there is a problem with my code. Please help!

The normal Handlebars list of users loads like it is supposed to but when I try to click on a person to get their detailed profile I get the following error after adding these two functions (which was not explained in the video, I had to look at the gitHub code):

Functions added or changed:

function getUserFilePath (username) { return path.join(__dirname, 'users', username) + '.json'; }

function getUser (username) { var user = JSON.parse(fs.readFileSync(getUserFilePath(username), {encoding: 'utf8'})) user.name.full = _.startCase(user.name.first + ' ' + user.name.last) _.keys(user.location).forEach(function (key) { user.location[key] = _.startCase(user.location[key]) }) return user }

app.get('/:username', function (req, res) { var username = req.params.username var user = getUser(username) res.render('user', { user: user, address: user.location }) })

ReferenceError: path is not defined at getUserFilePath (C:\Users\Cuong\Documents\GitHub\Learning-Express\index.js:9:3) at getUser (C:\Users\Cuong\Documents\GitHub\Learning-Express\index.js:13:41) at C:\Users\Cuong\Documents\GitHub\Learning-Express\index.js:51:14 at Layer.handle [as handle_request] (C:\Users\Cuong\Documents\GitHub\Learning-Express\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\Cuong\Documents\GitHub\Learning-Express\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\Cuong\Documents\GitHub\Learning-Express\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\Cuong\Documents\GitHub\Learning-Express\node_modules\express\lib\router\layer.js:95:5) at C:\Users\Cuong\Documents\GitHub\Learning-Express\node_modules\express\lib\router\index.js:281:22 at param (C:\Users\Cuong\Documents\GitHub\Learning-Express\node_modules\express\lib\router\index.js:354:14) at param (C:\Users\Cuong\Documents\GitHub\Learning-Express\node_modules\express\lib\router\index.js:365:14) at Function.process_params (C:\Users\Cuong\Documents\GitHub\Learning-Express\node_modules\express\lib\router\index.js:410:3) at next (C:\Users\Cuong\Documents\GitHub\Learning-Express\node_modules\express\lib\router\index.js:275:10) at SendStream.error (C:\Users\Cuong\Documents\GitHub\Learning-Express\node_modules\serve-static\index.js:121:7) at emitOne (events.js:116:13) at SendStream.emit (events.js:211:7) at SendStream.error (C:\Users\Cuong\Documents\GitHub\Learning-Express\node_modules\send\index.js:270:17)

My gitHub code for reference if needed: https://github.com/cu0ngpitt/Learning-Express/blob/Http-Verbs/index.js

Andy Elion
Andy Elion
~ 5 years ago

Bug in app.put method, should be

app.put('/:username', function (req, res) { var username = req.params.username var user = getUser(username) user.location = req.body.location saveUser(username, user) res.end() })