简体   繁体   中英

How to get node.js working with shared hosting server?

I am trying to get node.js and express.js based Restful Api running in a shared hosting account on Bigrock.

I was able to follow this article as a guide and get node installed on the shared hosting server. I am also able to run my application but for some reason the link doesn't work when I try to visit the page.

The app works perfectly on my local system. I suspect the problem is in the .htaccess file

.htaccess

RewriteRule ^$ http://127.0.0.1:3000P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ /Website/ [L]

app.js

const express = require('express')
const db  = require('../models/index');
const bodyParser = require('body-parser');
const path = require('path');
const Jobs = require('../models').Jobs;

const app = express();

app.use(bodyParser.json());

app.use('/api/jobs', require('../routes/jobs'));

app.get('/', (req, res) => {
    res.send("<h1>Hello World !</h1>")
})

app.listen(3000);

I should be able to view the Hello World when I visit https://www.example.com:3000

This is my directory structure

when https://www.example.com:3000/api/jobs is visited the app should be able to access the api, and when anyone visits https://www.example.com the person should be redirected to the website.

I have no prior knowledge of .htaccess and I would be very thankful for any suggestions.

Thanks.

You should not need the .htaccess file to run the Node application (unless you are using Apache2 as a reverse proxy of the Node process, which is unlikely).

You can check if your application is truly running by issuing a GET request from inside the host: curl localhost:3000 . It should return you something. If you don't have curl you can try wget .

If that works, then you should review the host settings to make sure that the port is open. After confirming this, you should check if the domain name "www.example.com" is resolving to the instance's IP name (which is a bunch of numbers in the form of 192.164.XX ). You can try accessing your instance's IP directly: 192.164.XX:3000 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM