I am using node.js with express for rest api and i have main file "server.ts" and i can write api routes in server.ts. But i wanna separate controller layer from server.ts even different controllers for their purpose. I have searched on google and found [link][1] for it.
[1]: http://bigspaceship.github.io/blog/2014/05/14/how-to-create-a-rest-api-with-node-dot-js/#toc_17 but i need this answer in typescript.
Related
I’ve been learning vue and im trying to understand what app.use() is used for is it anything similar to app.use in express?
I have looked at the vue he documentation and cannot find any helpfu information
It's for using vue plugins while in express it's registering middleware.
Here is the part of the docs: https://vuejs.org/guide/reusability/plugins.html#introduction
Trying to figure out how routing middleware works on ExpressJS.
What I am trying to solve is:
app has some basic routing in the /index.js
app will have some modules that will add its own routes and handlers (eg. hello & world)
I have added different routing options to see what works - but I have overcooked.
https://codesandbox.io/s/flamboyant-mcnulty-7b79x
Add the hello and world routes like this:
app.use(hello());
world.setup(app);
For some reason, the connect-history-api-fallback module will cause to nothing matched route.
If you comment the code use history middleware:
// app.use(history());
Now, you can access the hello route via https://ncpt4.sse.codesandbox.io/hello and access the world route via https://ncpt4.sse.codesandbox.io/world.
example link: https://codesandbox.io/s/amazing-worker-ncpt4
I am using express.js for my website. I have created the directory structure using 'express-generator'. It contains 'views' folder to render views. It also has 'routes' folder for routing purpose. Now my question is where should I define or do the core logic code?
For organization purposes, I use /controllers for my, well, controllers (which I believe is what you mean). Express really doesn't care where you put your code, just as long as you correctly use require('./path/to/whatever/directoryname/you/choose/filename') in your routes and views and module.exports = controllerToExpose; in your controllers.
I'm new to Sails, but have used Express, and am considering Sails for my upcoming project. I particularly like that it makes the CRUD API for me and connects Socket.io automatically.
The next application I'm planning to work on has an indeterminate size; if it works well, we want to separate our CRUD/JSON API from our Web/HTTP server and load balance the Web/HTTP server. This would allow us to utilize the CRUD/JSON API in other adjacent applications, like code for statistical analysis, or external data parsers which import data, or other things which have nothing to do with Web/HTTP servicing.
In express I would consider making the API section a module then export the express.router with all the api calls like so
//appAPI.js
var routes = require('express').router();
routes.get('/user/:id', function(request, reply){
// assume db is connected database object
// and request.params.id is as expected
db.getUser(request.params.id, function(u){
reply.json(u);
});
})
module.exports = routes;
//app.js
var app = require('express')(),
api = require('appAPI');
app.use('/api', api);
Then in my application, if I want to separate the Web from the API, I can package up the appAPI.js, and associated model code, and make a small connector to redirect all routes /api/* to the ip address and port of the api server, or other possibilities.
Can I do something like this in Sails? It seems that the automated model creation and the socket.io automation would make this difficult. Alternatively, I might be able to make a module for the API with a whole sails server then embed it in the main Web/HTTP server, which has its own sails objects running, but this seems like it either would not work, break the socket.io connections, or work, but be horribly inefficient as it would have multiple instances of sails running.
Any recommendations would be helpful and I'm willing to consider alternative ways of working this. Thank you all for any help you might provide and have a wonderful day.
I am totally new to everything Nodejs/express/angular, and I just ran into a question that bothers me.
When you have a MEAN stack, it seems that routes can be handled by both Express.js and Angular.
Angular:
For instance, if I define a route in Angular, I can do it like this:
var app = angular.module("app", []).config(function($routeProvider) {
$routeProvider.when('/login', {
templateUrl: '/templates/login.html',
controller: 'LoginController'
});
$routeProvider.when('/front', {
templateUrl: '/templates/front.html',
controller: 'FrontController'
});
$routeProvider.otherwise({redirectTo: '/front'})
});
But with express.js I do:
app.get('/',function(req,res){
res.sendfile('templates/angular.html');
});
So my question is:
When do you use angular routing, and when do you use express routing?
(I might miss something very obvious here, but I hope you can point it out)
Those two serve different purposes on a single page app.
The app would do all the CRUD (endpoints where you create/read/update/delete your stuff, for example: projects, users, bills, etc). Also it would do all the authentication stuff (like /login and /register).
All of that needs routes, because you would want something like /api/users to grab all your users. All those routes, AKA CRUD routes and authentication routes goes into express.js router. Why there? Because those are routes of the backend.
On the other hand, you have your angular application, which contains the visual part of your application and there you want some routes. You want / to point to your home, you would want /users to have a page where you list your users or even /users/add to have a page with a form to add new users.
You could see it this way:
Backend routes (express ones): Those are the routes that an end user won't have to know about or even use them (your angular app will use them to communicate with the backend to work with its data but an end user wouldn't put them directly on the browser)).
Frontend routes (angular ones): Are the routes that maps to different pages of your application and because of that, end users can use them to access some parts of your application directly.