ajax - How to redirect with expressjs for a specific status? -
i'm doing blog-app currently, , i'm struggling find way redirect/send specific status , act accordingly.
for example, have function saves data in mongodb using mongoose. if no errors occurred 200 status.
newarticle.save(function(err){ if (err) throw err; else { res.sendstatus(200); } });
i want able "fetch" status (i'm using react views , routes , superagent ajax request), , something, example, if article added load component on page have h1 saying : great job on posting article.
so first part.
the second part is, 404 or 500 errors want express redirect me example : myblog.com -> myblog.com/something , react router render basic 404 pages, not know how that, i'm searching lot , couldn't find something...
and, since lack knowledge in http basics how server , client talk each other, if have article/books recommend i'd know about.
for first part. depending on if using state or data library flux or redux, if not, can have ajax response have http status server. using that, can use setstate set state property called isarticlesavesucessful
. render success message component if key true.
second part. better user experience think intended, url should still user intended, ie, blog.com/bad-article-name page should render 404. similar above, when api response comes back, setstate accordingly, articlenotfound
. in render function, if
check on the state , if true, render error component.
Comments
Post a Comment