Sample code not working for my project
See original GitHub issueHello, 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:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
😉 On Jul 2, 2018, 2:56 AM -0600, Alessandro Capra notifications@github.com, wrote: