Thinkster MEAN Stack Tutorial problems with routing - javascript

I have been following this tutorial on thinkster.io:
https://thinkster.io/mean-stack-tutorial/
Everything works fine until I get to the section where we setup mongoose, then I start to hit problems.
First of all in the section "Creating Schemas with Mongoose" it says:
"Connect to our local MongoDB instance by adding the following code into our app.js file:"
But this is unclear - what do they mean by "our" app.js file - we no longer have an app.js that we created as we renamed it to angularApp.js - the only app.js file is the one generated by express/npm which is in the /views/ folder and is full of express configurations. I personally added the code to this file but am unsure if they meant for us to add it to /public/javascripts/angularApp.js
Secondly, in the "Creating our first route" section, the tutorial instructs us to add the code to routes/index.js but again this file is already populated with express routes and the tutorial is unclear as to whether we append this file with the new route for /posts or to remove the existing express route and add the /posts route instead.
Either way, the result is when I attempt to post the first data to mongodb via curl or postman it results in a 404, indicating the route is not working.
Any help appreciated - there is no comment section on the site for asking questions or troubleshooting, so I am hoping stackoverflow can fill in the gaps :)

Here's my code: https://github.com/jakblak/thinkster_mean_app
It's more organized then the original and commented.

Related

Mirage `passthrough` is not working

I'm trying to use passthrough for a POST request in this Ember project
this.passthrough('/thirdeye/entity?entityType=ANOMALY_FUNCTION');
This is what the call looks like in app/mirage/config.js.
I got the following error:
Mirage: Your Ember app tried to POST '/thirdeye/entity?entityType=ANOMALY_FUNCTION',
but there was no route defined to handle this request.
Define a route that matches this path in your
mirage/config.js file. Did you forget to add your namespace?
Clearly, I've added that call to the config file, but it's not being picked up. I read that passthrough only works for >= jquery 2.x, which my project is.
Does anyone know what else could cause this?
I found out the problem was I had to do this.passthrough('/thirdeye/***'); since the call has query params. It works now.

AngularJS: Retrieving Videogular templates stored in $templateCache results 404 Error

I currently develop an AngularJS 1.5.9 Single Page Application on my localhost with NodeJS running backend, where I use
Videogular framework. http://www.videogular.com/
Everything is fine except inserting videogular object on my page. I strictly follow the given example: http://www.videogular.com/examples/simplest-videogular-player/
<videogular vg-theme="controller.config.theme.url">
<vg-media vg-src="controller.config.sources"
vg-tracks="controller.config.tracks"
vg-native-controls="true">
</vg-media>
</videogular>
But it results in AngularJS error:
(sessionId in the request is the auth token not linked to current problem)
I have found the following in videogular.js :
$templateCache.put("vg-templates/vg-media-video", "<video></video>");
$templateCache.put("vg-templates/vg-media-audio", "<audio></audio>");
I have tried to store files locally, and the error disappeared
Actually there are a lot of plugins for Videogular and they all are using $templateCache to store some files in the cache, so it would be very confusing to manually store them locally in my app folder.
How can such files be stored in the cache using $templateCache so they can be extracted properly?
I apreciate your help.
UPDATE
I have tried insert $templateCache.get to directive, where the part is loading with error 404, and it works. But still doesn't work as it supposed to be.
It seems like there is an issue with sessionId that you pass in URL parametrs, do you actually need it?
I guess your interceptor or whatever auth managing module is wrong configured, so you don't check request target URL and id parameters are going to be added for local calls as well as for backend calls.

How do I show admin-only links in node.js?

Sorry if this is a rookie question but here it goes:
I want the Admin (when logging into the site) to view a link that non-admins can't view when they log in. I'm not even sure how to get started. I know I have to add two separate routes to the routes.js file. But not really sure how to display that in the UI or if I'm even correct.
This repo seems somewhat relatable to my problem. https://github.com/angular-ui/ui-router#get-started
Thanks
So the best way (using angular as you are) to create admin pages is to restrict them on your API within express or node.
Luckily there is a package for this! Passport a node module which can not only do regular logins but also google, facebook and many others.
I would take a look at there middleware examples then in your route you can do something as simple as this:
app.get('/admin', isAdmin ,function(req,res){
res.send('Secret Admin Things')
});
where isadmin will handle checking if they are an admin all in one swoop! Amazing tutorial for this.
I worked on a project which did not make use of passport and this is what they did
In the back end
res.locals = {
auth: req.session.auth,
}
res.render('index');
Then in the front end
HTML:
<span id="admin"> </span>
JavaScript:
if('{{auth}}' == admin){
menu = "<a href='/admin/users'> Admin: Users </a>"
$('#admin').append(menu)
}
Then to make sure someone could not look at the code to find the link and go to it you of course secure the back end
if (req.session.auth !== 'admin') { //
res.redirect('/');
}
The other answers are great if your using and know angular and passport. However, not everyone uses the same libraries so if your just using express and JQuery this option will work to get you started. There is much more to the code as far as security checks etc you would want to do but this is the basic way

NodeJs Express Router Error

I'm rewording this since I have figured out what the problem is, but I cant understand how its happening or how to resolve it.
Basically, in all of my routes I have to places where I am adding a DELETE route. The first is as follows
app.route('/bi/clubs/:id')
.post(authentication,clubController.getAll)
.delete(authentication,clubController.deleteClub);
The second location is
app.route('/bi/clubs/members')
.post(authentication,memController.getAll)
.delete(authentication,memController.deleteMember);
Whenever I call DELETE on the '/bi/clubs/members', express is actually routing that to '/bi/clubs/:id'. I have actually gone through and traced that this is occurring.
I can validate that I am not adding the route anywhere else in the app, and if I comment out the DELETE route on '/bi/clubs/id', then the second DELETE route will route properly. If I don't comment it out, the call to DELETE '/bi/clubs/members' will route to '/bi/clubs/:id'.
The order in which I am setting the routes is just as above.
Any help would be much appreciated.
Express is matching your route /bi/clubs/membersas /bi/clubs/:id. When a request is made to an Express app your app will start at the top of your routes and middleware and work its way down to the end hitting all the routes that it matches.
When you tell Express to match the route /bi/clubs/:id, all you are telling it is match bi, then clubs and then a dynamic value that you are referring to as id. Although you are probably looking for id to be a number or a MongoDB ID, Express doesn't know the difference, so technically the string members matches as a dynamic value. Just not the one you want.
If you console.log the value of id it should be members. Your static routes need to be registered before your dynamic routes.
If you have any questions or would like an example please let me know.

Listen for a webhook server side using meteor

I've set up a meteor app using iron-router and I want the app to listen to a webhook from another service (basically I'm building an API for other services to use)
So for example, when an external website calls myapp.meteor.com/webhook I want to catch that specific link and parameters and do stuff with the data.
Update: Thanks to a comment I found this: https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#server-routing
Router.route('/webhooks', { where: 'server' })
.post(function () {
console.log(this);
this.response.end('Caught you\n');
//this.response.status(200).json({text:"Todo added"});
});
I added the above in the /server folder as there is no need to for the front-end server to worry about this like mentioned in the comment. But when I load this using postman POST request, it just returns my HTML for not found. Any ideas?
Thanks in advance for your help.
UPDATE
I tried what #David said and still I get the template loaded and nothing in the console. Any idea what I'm doing wrong?
Your server route will only run if no client routes also match. I suspect you have a catch-all route which is executing on the client and preventing the server route from running. One solution is to define all of the routes in a common folder like /lib so that you can properly order them. Your routes file could look something like:
client route 1
client route 2
server route 1
server route 2
catch-all (not found) route

Categories