A
A
Alexander2014-06-24 16:37:58
IIS
Alexander, 2014-06-24 16:37:58

How to make two or more websites + backend in a node.js project and run under different domains?

I do not have super deep knowledge, so if you decide to answer, please elaborate. Perhaps I wrote complete nonsense, then I will be grateful for leading questions. Before that, I wrote on .net, but in node everything is a little different, therefore:
There is a project on node.js with one site and a server part. Everything is done in the express template, which is generated by default by express.js itself, i.e. the site is in the public folder, next to it there is a server.js file, which is launched by the npm server command. Next, I go to the localhost:8080 page and see the site.
On hosting, I also run npm server, in IIS I set up a redirect from mydomain.ru to localhost:8080 and everything is ok.
It's time to write another site that will use the same server part and then I don’t understand anything at all, but there should be two different domain names admin.mydomain.ru and mydomain.ru, and maybe more, for example, mobile.mydomain .ru
How to do it beautifully and correctly?
Hosting under iis, please do not discuss, just take it for granted.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2015-04-28
@AlexanderKot

I'll write my version that works. Not 100% detailed and maybe even not super nice and correct, as originally dreamed, but I think it will help noobs with poor English to understand in which direction to think. Otherwise, similar advice can be found on the stackoverflow.
I decided to do this: I launch the node process of the web application + the node process for its api (restful type). (There will be 4 processes for 2 sites, etc.) Of course, in the form of Windows services using https://nssm.cc/.
I create a site in IIS, set a domain name for it, insist 2 redirects to ports - 3004 for api and 3003 for a web application. To do this, you need to put 2 pribludy in iis:
www.iis.net/downloads/microsoft/application-reques...
www.iis.net/downloads/microsoft/url-rewrite
Then, to configure rewrite - select a site and click on the [Url rewrite] icon, after [Add rule] and select reverse proxy, specify the type path http://localhost:3003and OK
IIS will ask about enabling ARR, say yes. After that, you can throw such text in web.config, it should work.

<configuration>
<system.webServer>
...   
<rewrite>
    <rules>
        <clear />
        <rule name="images">
            <match url="photos/.*" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
            <action type="None" />
        </rule>
        <rule name="api" stopProcessing="true">
            <match url="api\/.*" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
            <action type="Rewrite" url="http://localhost:3004/{R:0}" />
        </rule>
        <rule name="web" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
            <action type="Rewrite" url="http://localhost:3003/{R:0}" />
        </rule>
    </rules>
</rewrite>

The main constipation, for some reason, was with IIS. I didn’t ride a moped, although everything seems to be simple there, it was badly googled, and what exactly didn’t work.
node web application process run on port 3003, the main application file has routing like this
app.use('/', express.static(__dirname));  // не уверен, что это нужно
app.use('/js', express.static(__dirname + '/js')); // у меня express.js обрабатывает статику, мне показалось так проще, чем в iis настраивать, иначе на каждый запрос скрипта, стиля и т.д. iis будет пулять Index.html
app.use('/libs', express.static(__dirname + '/libs'));
app.use('/img', express.static(__dirname + '/img'));
app.use('/css', express.static(__dirname + '/css'));
app.use('/photos', express.static(config.photoPath)); // про фото не уверен, что именно работает - правило в iis или это
//во всех остальных случайх возвращается index.html со всеми js файлами spa приложения
app.all('/*', function (req, resp, next) {
    resp.sendfile('index.html', {root: __dirname});  //если при таком подходе, вам в ответ на какие-либо запросы лезет этот index.html, значит либо rewrite в iss не заработал, либо обработка статики в экспрессе с ошибкой сделана.
});

node process with an application that is api run on port 3004
Routing will be written in the main file like this:
app.use('/api/category', yourCategoryHandler);

S
sasha, 2014-06-24
@madmages

Well, if I understand the question correctly, then the answer is:
when requesting a domain, the request redirects to the IP that you specify in the registrar panel. In the node, already check which domain was requested (stupid if() ) and, depending on the domain, do what you need to do

A
Alexander Keith, 2014-06-25
@tenbits

If iis is used, then I recommend the https://github.com/tjanczuk/iisnode module, then IIS will be responsible for the start and distribution - where you actually create sites and bindings as usual.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question