2

So I'm at a lose here and really need some direction.

I've been following Microsofts 'Introduction To NodeJS' course on edX. We had to create a very simple RESTful blog api using express. I've been trying to get it to run on my namecheap shared hosting.

What I've done:

  • Followed these instructions to install nvm with the latest stable version of node.
  • Followed this question to get my express app to work with https
  • Uploaded the app to the hosting server.
  • SSH'd into the hosting server, ran npm install.
  • ran node server.js to start the server. Seems to start okay.
  • Try navigating to my website on the port the app is listening on https://samkeene.co.uk:3000/blog-api
  • Expecting to see "Hello World" but instead getting a timeout.

server.js

const routes = require('./routes');
const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');
const errorhandler = require('errorhandler');
const https = require('https');
const fs = require('fs');
const path = require('path');

let store = {
    posts: []
};

let key = fs.readFileSync(path.join(__dirname,'../../ssl/keys/c07f7_003d1_ecb06ccb2afcd72cfa43b6011c82464e.key'));
let cert = fs.readFileSync(path.join(__dirname,'../../ssl/certs/samkeene_co_uk_c07f7_003d1_1574035199_5b90fc5e96ac0c534d2ee116af6fd342.crt'));
let options = {
    key: key,
    cert: cert
};

let app = express();
app.use(bodyParser.json());
app.use(logger('dev'));
app.use(errorhandler());

app.use((req, res, next) => {
    req.store = store;
    next();
});

app.get('/', (req, res) => {
    res.send('hello world')
});

app.get('/posts', routes.Posts.getPosts);
app.post('/posts', routes.Posts.addPost);
app.put('/posts/:postID', routes.Posts.updatePost);
app.delete('/posts/:postID', routes.Posts.removePost);

app.get('/posts/:postID/comments', routes.Comments.getComments);
app.post('/posts/:postID/comments/', routes.Comments.addComment);
app.put('/posts/:postID/comments/:commentID', routes.Comments.updateComment);
app.delete('/posts/:postID/comments/:commentID', routes.Comments.removeComment);

let server = https.createServer(options, app);

server.listen(3000, () => {
    console.log("server starting on port : " + 3000)
});

The rest of the files can be found in its git repo. (or navigate to https://www.samkeene.co.uk/blog-api/)

Would really appreciate some help.

Edit: I decided to just try if I can get even simpler app to run:

server.js

const express = require('express');

let app = express();

app.get('/', (req, res) => {
    console.log("hello");
    res.send('hello world')
});

app.listen(3001);

This runs into the same trouble! Connection timeout. I'm really at a lose here.

AlwaysNeedingHelp
  • 1,851
  • 3
  • 21
  • 29

2 Answers2

0

Might have to do with the port you are listening on.

For http listen on port 8080

For https listen on port 443

Rastalamm
  • 1,712
  • 3
  • 23
  • 32
0

Was scratching my head about this one too for a while once I was on a shared hosting.

I managed to run the following by setting up a NodeJS app via cPanel with NodeJS version 9.11.2:

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

app.get('/', function(req, res) {
    res.send('Howdy! req.url: '+req.url);
});
app.get('/users', function(req, res) {
    res.send('Should send a bunch of users!');
});
app.get('/things/:name/:id', function(req, res) {
    res.send('id: ' + req.params.id + ' and name: ' + req.params.name);
});
app.listen();

I didn't have to care about port numbers and was able to access these end points via HTTPS too.

In your case, just remove 3001 and see if it works with your shared hosting.

Hope it helps. :)

taiff
  • 89
  • 8