Register with meteor using express - node.js

I want to call Accounts.createUser on my register routing, i pretty sure i'm doing something wrong.
app.post('/register', (req, res, next) => {
Accounts.createUser(email,password,username)
}
Is there a way to call a meteor method on express ??

If you are needing integrate your Express backend with your Meteor accounts, I would recommend you look account-js (as a decoupled way of manage accounts like Meteor accounts). It is possible integrate your user accounts between Meteor and Express, as an interesting use case.
Take a look at it: https://accounts-js.netlify.com
Github project: https://github.com/accounts-js/accounts
Meteor integration: http://accounts-js.netlify.com/docs/cookbook/meteor
Enjoy!

Related

Creating a Node.js REST API using Firebase Cloud Functions, without Express?

I am working to create a serverless REST API via Firebase Cloud Functions, which seems to work well but the examples and documentation all seem to use a monolithic solution, since they use the Express framework and essentially map the root http request to the Express app, then let it handle the routing. I understand that this is because the Firebase Hosting platform does not have the ability to handle http verbs.
My expectation was that a serverless / FaaS approach would have a function for each endpoint, making for easy updates in future since there's no need to update the whole app, just that single service - i.e. a more functional approach.
What am I missing here? Why is the approach to use a single function to contain an express app? Doesn't this defeat the purpose of a serverless / Cloud Functions approach? And is there any other way of doing this?
The documentation shows how to create an endpoint without the help of an Express app, router, or middleware:
exports.date = functions.https.onRequest((req, res) => {
// ...
});
All you have to do is arrange to send a response with res.send(...) or similar.

Serving Angular2 app at a base URL with Express

I have an Angular2 app up and running and a very simple express server. Is it possible to only serve my application when a user visits '/app' (for example)?
If yes how can it be implemented?
I intend to have multiple Angular apps on different URLs and all of these need scripts. Hence a solution which takes care of which script to handle when would be desired.
Edit- Along with the accepted answer, if using the angular-cli, the 'base-href' has to be set to the URL of the app when building the app.
Yes, that's possible.
You have to define that endpoint as the only one:
app.get('/app', function (req, res) {
res.send(/* your application */)
})
You can use this:
// serve the angular web page
app.get('/', app.use(express.static(path.join(__dirname+'/../../public'))));
Note: change the dir name to reach your directory where contains the angular static files.

I am using loopback to scaffold an application, can I still write my code as an express application instead of using some of their components?

I have used loopback to scaffold out my application but want to use some of their components, their documentation is lacking, can I just enable the functionality I want like I would using just express?
Specifically, I want to implement OAuth2
The loopback object is directly the express object (with all the loopback sweet additions of course). You can obtain this object by requiring the server.js file scaffolded in your application.
So you can do everything express does with it, like routing for instance
var app = require('server.js').
app.get('/', function (req, res) {
res.send('Hello World!');
});
Implementing OAuth2 is absolutely possible (done it myself, without using oauth loopback plugin), nothing preventing it, but it's not exactly straightforward. I would advise to spend some time getting familiar with loopback and building a few small applications first.

KeyStone JS Account Controller

I understand MVC structure when coding in NodeJS. I started using Keystone JS recently, and I really like it. But, the way they set their controllers up, it seems that the controllers ONLY serve the purpose of rendering a view.
In an earlier project, I had an Account.js model and an Account.js controller. I'm trying to see how it would copy over to keystone.
So: How would I allow users to signup/signin/logout in a Keystone project (not into the Admin UI, but like a member of a regular site)? How would I make an Account controller (obviously with no view to render)?
There are lots of ways you can implement your own methods of authentication and account management in keystone since it is based on express.js.
You can then add an array of 'middleware' functions to routes which will run before passing the request to the controller.
e.g
Route before middleware added
app.get('/admin', routes.views.userAdmin);
Middleware Function
function isAuthenticated(req, res, next) {
// do any checks you want to in here
// CHECK THE USER STORED IN SESSION FOR A CUSTOM VARIABLE
// you can do this however you want with whatever variables you set up
if (req.user.authenticated)
return next();
// IF A USER ISN'T LOGGED IN, THEN REDIRECT THEM SOMEWHERE
res.redirect('/');
}
Route with middleware added
app.get('/admin', isAuthenticated, routes.views.userAdmin);
It's a very broad questions so I'd recommend you go and decide on the best way you'd like to do it yourself as everyone has their own personal preference. The search terms you want are 'express middleware authentication'. A lot of people use PassporJS http://passportjs.org/
Hope that helps :)

How should I run a REST API with Meteor?

I need the Meteor server to handle a very simple POST request not coming from the application client. With Express, I'd just do something like app.post('/something', function....
Is there an equivalent in Meteor? If not, how should I set this up, startup an Express server in a is_server context?
Meteor does not yet have the built in functionality to provide a restful API.
You can build basic routing into our application using Backbone, as in the Meteor example provided here: http://meteor.com/examples/todos
You can do something like this:
var AppRouter = Backbone.Router.extend({
routes: {
"": "dashboard",
"home": "dashboard",
"profile": "profile",
},
profile: function () {
Session.set("current_view", "profile")
this.navigate('profile', {trigger: true});
},
Also take a look at: How to expose a RESTful Web Service using Meteor
Alternatively you can serve RESTful APIs with Meteor using the meteor-collectionapi Atmosphere package. See also Is Meteor an option, if i need an additional REST API?.

Resources