question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sample code not working for my project

See original GitHub issue

Hello, I used the sample code provided in the readme with no success. Perhaps my project needs some adjusting, but I do not know how to debug what’s wrong since no errors are given.

Here is my Express server code:

const express = require('express');
var sslRedirect = require('heroku-ssl-redirect');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const exphbs  = require('express-handlebars');
const app = express();

const routes = require('./routes/index');
const users = require('./routes/user');
const sensor = require('./routes/sensor');

const db = require('./models');

const env = process.env.NODE_ENV || 'development';
app.locals.ENV = env;
app.locals.ENV_DEVELOPMENT = env == 'development';

// view engine setup

app.engine('handlebars', exphbs({
  defaultLayout: 'main',
  partialsDir: ['views/partials/']
}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'handlebars');

// app.use(favicon(__dirname + '/public/img/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// enable ssl redirect
app.use(sslRedirect());

app.use('/', routes);
app.use('/users', users);
app.use('/sensor', sensor);

/// catch 404 and forward to error handler
app.use((req, res, next) => {
  const err = new Error('Not Found');
  err.status = 404;
  next(err);
});

/// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use((err, req, res, next) => {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err,
      title: 'error'
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use((err, req, res, next) => {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {},
    title: 'error'
  });
});

app.set('port', process.env.PORT || 3000);

db.sequelize.sync().then(function() {
  let server = app.listen(app.get('port'), function() {
    console.log('Express server listening on port ' + server.address().port);
  });

  // socketIO integration
  let io = require('socket.io').listen(server);

  // listen for a connection request from any client
  io.on('connection', function(socket){
    console.log('a user connected');

    socket.on('disconnect', function () {
      console.log('user disconnected');
    });

    socket.on('p', function(value){
      console.log("pressure: " + value.p);
      io.emit('pc', value);
    });

    socket.on('renewSocketConnection', function(value){
      // console.log("value received from sensor is " + value.pressure);
    });
  });

  exports.server = server;
  exports.io = io;

});

module.exports = app;

Any suggestion on what might be not working? Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
paulomcnallycommented, Jul 2, 2018

Try move

// enable ssl redirect app.use(sslRedirect());

Before

// view engine setup

And try again On Jul 2, 2018, 2:46 AM -0600, Alessandro Capra , wrote:

From my package.json: “express”: “^4.13.3” — You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

0reactions
paulomcnallycommented, Jul 2, 2018

😉 On Jul 2, 2018, 2:56 AM -0600, Alessandro Capra notifications@github.com, wrote:

Closed #10. — You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Studio, Sample Code - Not working - Stack Overflow
Unpack the downloaded project package. · In Android Studio, chose File > Import Project and select the root folder of the unpacked project....
Read more >
Java project management in Visual Studio Code
For example, you can switch between hierarchical view and flat view. ... You can export your build to JAR from the projects view...
Read more >
Visual Studio Code Tutorial for Beginners - Introduction
Academind is your source for online education in the areas of web development, ... No matter if you are looking for a tutorial,...
Read more >
Tutorial: Open a project from a repo in Visual Studio
In this tutorial, you'll use Visual Studio to connect to a ... how to use your GitHub account to sign in, see the...
Read more >
Step 1. Create and run your first Python project - JetBrains
If PyCharm detects no Python on your machine, it provides the ... can go on with your code; a yellow light means some...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found