0
votes

I'm new to Node.js. Started following Sahat Yalkabov's tutorial on creating a Node.js Express + React.js app.

I connected to my server with SSH (PUTTY) and forwarded port 3000 to localhost:3000. However, when I try to connect to the server with my browser it doesn't respond and the browser just shows waiting for localhost message.

I have previously ran express command and their default project did show on my browser, so it's not a firewall thing.

Here is server.js:

var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');

// Server-side React rendering
var swig  = require('swig');
var React = require('react');
var ReactDOM = require('react-dom/server');
var Router = require('react-router');
var RoutingContext = Router.RoutingContext;
var routes = require('./app/routes');

var app = express();

app.set('port', process.env.PORT || 3000);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

// Server-side React rendering
app.use(function(req, res) {
  Router.match({ routes: routes, location: req.url }, function(err, redirectLocation, renderProps) {
    if (err) {
      res.send(500, err.message)
    } else if (redirectLocation) {
      res.redirect(302, redirectLocation.pathname + redirectLocation.search)
    } else if (renderProps) {
      var html = ReactDOM.renderToString(<RoutingContext {...renderProps} />);
      var page = swig.renderFile('views/index.html', { html: html });
      res.send(200, page);
    } else {
      res.send(404, 'Page Not Found')
    }
  });
});

app.listen(app.get('port'), function() {
  console.log('Express server listening on port ' + app.get('port'));
});

To run the server, I'm using 2 terminals, one running gulp and the other npm run watch, as per said tutorial.

I'm really new to all of this, first time running Node.js. Any help would be much appreciated!

EDIT: Added a git repository with my project, here.

1

1 Answers

0
votes

I think you should only run npm run watch command. After terminal shows Express server listening on port 3000, please open http://localhost:3000/ on your browser.