I have a an existing website built with express and I would like to add a "/blog" powered by Ghost. I've added Ghost to my dependencies, installed and configured the urls in Ghosts config to localhost:3000/blog, but now I'm having trouble.
In my app.js I've added the following lines:
var blog = require('./routes/blog');
app.use('/blog', blog);
My blog.js looks like this:
var express = require('express');
var router = express.Router();
var ghost = require('ghost');
ghost().then(function (ghostServer) {
ghostServer.start();
});
router.get('/', ghost);
module.exports = router;
I'm pretty sure blog.js is incorrect.
Node is very limited to do is, for cases when ghost is not configurable as express middleware, which I believe is the case here.
That leaves you with Loadbalancers and DNS as solutions to this problem. On something like HAPRoxy or Nginx you could make those recirects on the /blog route, would need to cater for scripts that the HTML requires to load and to redirect them too.
This might be also better practice since you seperate the concerns.
Related
Hie,
I am developing a Nodejs (Express) web app and pretty much new to this technology. So far I see that there can only be one point of entry mine being my the server.js file. Now it seems all requests and/or processes should be initiated here which is fine for a smaller application, but my site has about 25 page routes already all of who's request should be handle here. I also have a dozen or so Ajax requests are handled here. Now even though I am processing different functions e.g CRUD operations in separate files, I still fear at some point my code will become unreadable as the server.js file get longer
const express = require("express")
const path = require("path")
const exphbs = require("express-handlebars")
let app = express()
app.set("views",path.join(__dirname,'templates'))
app.engine('handlebars',exphbs({defaultLayout:'main'}))
app.set('view engine','handlebars')
app.set('port',(process.env.PORT || 3000));
app.get('/',(req,res)=>{
res.render('home',{'title':'Home'});
});
app.get('/home',(req,res)=>{
res.render('home',{'title':'Home'});
});
app.get('/register',(req,res)=>{
res.render('register',{'title':'Register'});
});
app.use(express.static(path.join(__dirname, '/public')));
app.listen(app.get('port'),()=>{
console.log(`Server started on port : ${app.get('port')}`)
})
So far my server.js is this small, but it just hit me that I have 25 pages and multiple Ajax processes on each.
Yes, you have to structure your routes. For that, you have to look at Express Router. You have to create different route files based on a specific resource.
/routes/homeRoutes.js
const express = require("express");
const router = express.Router();
router.get('/',(req,res)=>{
res.render('home',{'title':'Home'});
});
module.exports = router;
server.js
const homeRoutes = require("./routes/homeRoutes");
app.use("/api/v1/home", homeRoutes);
Also, have a look at the following links for a better understanding of project structure and express router.
https://expressjs.com/en/guide/routing.html
project structure
I think what you are looking for is splitting the code up in local modules. You can place parts of your code in separate files, include module.exports at the end and then require(./filename.js) them in your server.js.
You can see an example here: https://www.tutorialsteacher.com/nodejs/nodejs-local-modules
I wonder how webpages like support.domain.com created?
I have below code which creates routing like
domain.com/support.
Any answer will be really helpful.
app.use('/support' ,express.static(path.join(__dirname,'support')));
Do I need to own the domain support.domain.com ? I already own the domain.com
You can use an third party model named "express-subdomain"
And apply it as of one of your Express middleware
const subdomain = require('express-subdomain');
const express = require('express');
const app = express();
const supportRoute = require('./routes/support');
app.use(subdomain('support', supportRoute));
[Development]
And don't forget you modify your /etc/hosts/
localhost 127.0.0.1
support.yourapp.local 127.0.0.1
[Production]
And for production, you have to add a new a record for your DNS domain
I am following an on-line tutorial of implementing in MEAN. Everything looks great. Except when it comes to routes. I understand that routes need to be in a javascript files (js extension). It's okay with a small web site. But as the number of requests grow, I would like to put them in separate files. I looked up in SOF for how to include files in Javascript. It is non-trivial. Has anyone faced this issue before? Can anyone comment?
Thanks
You can use Router Middleware by using express.Router(). This allows you to break your routes into different files. On a side note, middleware is very powerful and is worth learning about, its a huge part of Express.
Say you have an app that has a /users section. You can create a separate routes file called users.js that contains all routes that pertain to your /users resources. Then inside your server.js where your main Express app is listening, you can assign the users.js routes to the /users resource using app.use().
You can have as many routers as you'd like, all routes are read top-down when Express is deciding which route to use.
./routes/users.js
// Create an express router
var router = require('express').Router();
// Define a route for GET /
router.get('/', function(req, res) {
res.status(200).send('Welcome to /users');
});
// make our router available for require() statements
module.exports = router;
server.js
var express = require('express');
var app = express();
// Users routes
var users = require('./routes/users');
// Tell our app to use the Users routes defined in ./routes/users.js
app.use('/users', users);
app.listen(process.env.PORT || 3000, function() {
console.log('listening');
});
I just set up a NodeJS server and wanted to use the vhost function from Express to allow for easy project setup. I want to be able to create a new directory for a new project without going through the hassle of creating new subdomains etc.
Basically, I want to be able to reach project1 at node.domain.com/project1 and project2 at node.domain.com/project2.
I'm now running server.js, which is located in the root (node.domain.com/server.js) and contains:
var express = require('express');
var app = express();
app
.use(express.vhost('node.domain.com', require('./project1/app.js').app))
.use(express.vhost('node.domain.com', require('./project2/app.js').app))
.listen(3000);
It's all working fine, when I go to node.domain.com/project1 I get the results of ./project1/app.js but whenever I change something in any of the app.js files it requires a restart (Ctrl+C followed by node server.js) for it to update the changes.
And the contents of app.js are, for example:
var express = require('express');
var app = exports.app = express();
app.get('/project1', function(req, res){
res.send('Hello World [/project1]');
});
Any idea why this is?
A thing to note is that I also run Apache on the same server, and I'm using this guide to allow both servers to run on port 80 (but accesible via different subdomains).
Oh! I feel dumb now, seems like I need to use something like forever, with its w flag.
I'm trying to serve some static file with a nicer url endpoints.
For example, /home will serve /public/home.html.
I can probably use res.sendfile() in the route config, but sendfile will not cache the file output in production mode so I am not too sure if this is a good solution.
How do I set routes to act like an alias to a html file?
Try this.
var express = require('express');
var app =express.createServer();
// ------
app.get('/home', function(req,res){
res.sendfile(__dirname + '/public/home.html');
});
There's a module called node-static that provides cacheing functionality. You also might be able to just use symlinks. You might be better served with nginx's try_files or another non-node.js reverse proxy for this functionality.
For a while, there was a staticCache middleware built into express that would do the cacheing. It was removed from connect in January 2014.Here's the github issue where TJ explains staticCache being deprecated.