开发者

How can I configure multiple sub domains in Express.js or Connect.js

开发者 https://www.devze.com 2023-02-28 15:17 出处:网络
I am used to working开发者_运维技巧 on httpd ( Apache ) which provides a way to configure subdomains which is mapped to a directory.

I am used to working开发者_运维技巧 on httpd ( Apache ) which provides a way to configure subdomains which is mapped to a directory. How can I do the same thing in Connect.js/Express.js ? I see that the only thing that I have is routes which I am not sure how I can use to configure sub domains. I have subdomains like m.mysite.com, sync.mysite.com

Can someone help ?


Or alternatively you could use vhost.

Then, create several sites in their own directory and export the express app, eg. /path/to/m/index.js:

var app = express()
/* whatever configuration code */
exports.app = app
// There is no need for .listen()

And then handle all requests with the following app:

var vhost = require('vhost');

express()
.use(vhost('m.mysite.com', require('/path/to/m').app))
.use(vhost('sync.mysite.com', require('/path/to/sync').app))
.listen(80)

Note that /path/to/m and /path/to/sync can be absolute paths (as written above) or relative paths.


You could append a subdomain to a request and then check for it in subsequent next() calls.

I got the following code from > http://groups.google.com/group/express-js/browse_thread/thread/b04bbaea7f0e8eed (so full credit to the original author)

app.get('*', function(req, res, next){ 
  if(req.headers.host == 'some.sub.domain.com')  //if it's a sub-domain
    req.url = '/mysubdomain' + req.url;  //append some text yourself
  next(); 
});

// This will mean that all get requests that come from the subdomain will get 
// /subdomain appended to them, so then you can have routes like this 
app.get('/blogposts', function(){ 
  // for non-subdomain 
});

app.get('/mysubdomain/blogposts', function(){ 
   // for subdomain 
});


I have recently came across this problem, and wrote a module to help with it using express 4. https://www.npmjs.org/package/express-subdomain.

Example - api subdomain.

var express = require('express');
var app = express();

var router = express.Router();

//api specific routes
router.get('/', function(req, res) {
   res.send('Welcome to our API!');
});

router.get('/users', function(req, res) {
    res.json([
        { name: "Brian" }
    ]);
});

app.use(subdomain('api', router));
app.listen(3000);

Check out the module on npm to see more examples.


I created a module to help with subdomains in Express: https://github.com/WilsonPage/express-subdomain-handler


Do as I say, create two express app in different folder.

For example: one app in /blogsite directory

const express = require("express");

const blog = express();

blog.get("/", (req, res) => {
	res.send("BLOG SECTION");
});

blog.get("/allblogs", (req, res) => {
	res.json([
		{ title: "csgo major boston", description: "Best CSGO major ever" },
		{ title: "Blast pro series", description: "Coolest series of CSGO" },
	]);
});

module.exports = { blog };

and another one in /portfolio directory

const express = require("express");

const portfolio = express();

portfolio.get("/", (req, res) => {
	res.send("PORTFOLIO SECTION");
});

portfolio.get("/resume", (req, res) => {
	res.send("HERE'S MY RESUME");
});

module.exports = { portfolio };

Now create a main app in the outer folder and import the other two express apps that you just made in /blogsite directory and /portfolio directory.

And in the main app do this,

const express = require("express");
const vhost = require("vhost");

const { blog } = require("./blogsite");
const { portfolio } = require("./portfolio");

const app = express();

// BLOG AND PORTFOLIO

// url: http://blog.localhost:3002/
// url: http://blog.localhost:3002/allblogs

// url: http://portfolio.localhost:3002/
// url: http://portfolio.localhost:3002/resume

app
	.use(vhost("portfolio.localhost", portfolio))
	.use(vhost("blog.localhost", blog));

// MAIN APP ROUTES OR ENDPOINTS

// url: http://localhost:3002
// url: http://localhost:3002/myhobbies

app.get("/", (req, res) => {
	res.send("MAIN APP SECTION");
});

app.get("/myhobbies", (req, res) => {
	res.send("MAIN APP -> myhobbies section");
});

app.listen(3002, () => {
	console.log("started listening");
});

fileStructure at the end should be looking like this

main(folder)
  index.js (main express app which you need to run using node or nodemon)
  blogsite(folder that I talked about for blog.localhost)
    index.js (blog express app)
  portfolio(folder)
    index.js (portfolio express app)


I've had this exact same requirement for a project I was working on and ended up throwing together a middleware-based solution. It allows you to define routers and view folders per subdomains.

Check it out on NPM https://www.npmjs.com/package/express-multiview or GitHub https://github.com/daryl-cecile/express-multi-view#readme

0

精彩评论

暂无评论...
验证码 换一张
取 消