How to mount a Sails.js app as express middleware - node.js

I would like to use Sails within a larger express based app. Most node.js MVC frameworks I have worked with you can mount as express middleware. Is this possible with Sails?
I want to do something like:
var express = require('express'),
app = express();
var mySailsApp = require('./mysailsapp');
app.use(mySailsApp);

While there are some active efforts to develop systems that would let Sails be more modularized, there's no way to use a Sails app as Express middleware. Sails works on top of Express, but not the other way around. However, you can use custom Express middleware with Sails--see this answer for an example.

Related

Serving static files in Express (express.static vs app.static)

Through serving static files in Express .. I saw below code:
const express = require('express');
const app = express();
// Initialize the main project folder
app.use(express.static('website'));
Why we didn't use app.static() instead of express.static() as we already assigned express() to the app constant, and what is the difference between them?
Note: I tried to replace express with app and it said app.static is not a function. I also saw some NPM packages that use app.static() like wamjs for example, which is weird.
app.static() has nothing to do with Express.
Wam is a completely different framework (that may be Express-like in some ways, but it's not Express and not identical to Express). Here's a description on the NPM wam.js page:
Wam is a small koa and next.js inspired middleware framework for node.
If you want to program with Express, then use the Express documentation, not the Wam documentation and it will guide you to use app.use(somePath, express.static()). You can see in the Express doc for the app object, there is no mention of app.static(). That is apparently something that wasm.js invented for it's own framework.
Why we didn't use app.static() instead of express.static() as we already assigned express() to the app constant, and what is the difference between them?
Because Express doesn't have app.static(). It has express.static().
I also saw some NPM packages that use app.static() like wamjs for example, which is weird.
I wouldn't call it weird. wamjs is a different package with a different API. It is not Express so there should be no expectation that Express behaves like wamjs or that wamjs behaves like Express. They are different frameworks.

LocalHost to Domain Name using Express in React using nodejs

I build a nice React website for myself that's using Node Js and Mongodb on the backend. I finished everything and tested my mongodb atlas connection and everything is working fine on that side.
I was using Express to create a server, developing using localhost:4000. Now that I actually have a domain called let's say https:something.com using AWS amplify, I don't understand what modifications I have to make. Do I have to make Express listen to that url? How do I do that?
const express = require('express')
const app = express()
const port = 4000
var bodyParser = require('body-parser')
..... GET and POST REQUESTS
app.listen(port,()=>{console.log("Listening on port 8000")});
I've been searching a lot but I couldn't find anything useful. Or am I not supposed to be using Express? Am I supposed to do something like this:
app.listen(port,"IP");
You don't have to do anything, the custom domain is for frontend, so you don't need to change anything in your express code.

Do I need to use morgan logger in production if my express app runs on heroku?

I'm using papertrail to view my logs. Will using morgan as an express middleware just slow things down? Does it serve any purpose?
Thanks

node js using express and restify together in one app

I am using restify building apis, it works great. But I need to render some web pages as well in the same application.
Is it possible I can use express and restify together in one application?
this is the code for restify server in app.js
var restify = require('restify');
var mongoose = require('mongoose');
var server = restify.createServer({
name : "api_app"
});
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.use(restify.CORS());
mongoose.connect('mongodb://localhost/db_name');
server.get('/', routes.index);
server.post('/api_name', api.api_name);
server.listen(8000 ,"localhost", function(){
console.log('%s listening at %s ', server.name , server.url);
});
how do I create express server in the same app.js?
Thanks
For all intents and purposes restify and express can't coexist in the same node process, because for unfortunate reasons, they both try to overwrite the prototype of the http request/response API, and you end up with unpredictable behavior of which one has done what. We can safely use the restify client in an express app, but not two servers.
I think restify, like express, simply creates a function that you can use as a request handler. Try something like this:
var express = require('express'),
restify = require('restify'),
expressApp = express(),
restifyApp = restify.createServer();
expressApp.use('/api', restifyApp); // use your restify server as a handler in express
expressApp.get('/', homePage);
expressApp.listen(8000);
If you need REST APIs and normal web pages, I don't think you need to stick to restify any more. You can always just use express, and build your API on it instead of restify, since express can do almost all things restify does.
I would solve this with a local api server, and a express proxy-route. Maybe not the best way with a bit of latency, but a possible solution how to separate your web frame from your api.
If you need to do a REST API and a Web application, I recommend you to use a framework that works on Express.
I created the rode framework, that is excellent to work with REST and works with Express.

Node.js, Express, Jade & Template in Package

Is it possible to store templates for an express application in a separate package?
In my usecase I'd like to have a shared package containing global templages to give all apps the same look and feel even so they run as an independent entity on another port or even another server. Local content templates could live within the app, so all I'm looking for is a way to share that kind of code between multiple apps.
Going a step further I was thinking about skinning packages which can overwrite the default templates. Once installed in the "template package" it could change the look and feel of all applications using the core templates.
Is there a way of doing that without having to drop the comfort of express?
cu
Roman
This is possible using express. You can basically mount a whole app object to a specific route (with all routes and middleware).
var express = require('express');
var coreApp = express();
var blogApp = express();
var wikiApp = express();
// init blogApp and wikiApp with middleware and routes
coreApp.use('/blog', blogApp);
coreApp.use('/wiki', wikiApp);
Now you can mount your templates into this modular apps and then mount them into your core app.
Here's a screen cast from the express creator himself, called modular web applications.

Resources