1

I try to show a (static) HTML webpage via Heroku. I have followed this tutorial: https://www.youtube.com/watch?v=gAwH1kSODVQ but after many attempts it is still not working.

I'm rather new to coding, so if you can give concrete examples that would be great!

The following files have been pushed to heroku:

server.js 
package.json
Procfile.js
(folder) public with index.html, main.css

//Server.js file:

var express = require('express'); //require express module in server.js file
var app = express();
var mongojs = require('mongojs');
var db = mongojs('birthdaylist', ['birthdaylist']);
var bodyParser = require('body-parser');
var http = require('http');
var port = Number(process.env.PORT || 3000);

app.use(express.static(__dirname + '/public')); //connect to html file
app.use(bodyParser.json());

app.get('/birthdaylist', function(req, res) {
  console.log("The server has received a GET request.")
  db.birthdaylist.find(function(err, docs){
    console.log(docs);
    res.json(docs);
  });
});

app.post('/birthdaylist', function(req, res){
  console.log(req.body);
  db.birthdaylist.insert(req.body, function (err, doc){
    res.json(doc);
  });
});

app.delete('/birthdaylist/:id', function(req, res){
  var id = req.params.id;
  console.log(id);
  db.birthdaylist.remove({_id: mongojs.ObjectId(id)}, function(err, doc){
    res.json(doc);
  });
});


app.listen(port, function () {

});

1 Answer 1

1

you should use:

app.listen(%PORT_NUMBER%, function () {
  // some code here
});

Instead of:

var server = http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type':'text/html'});
    res.end('<h6>Hello worldsfasfd!</h6>');
});
Sign up to request clarification or add additional context in comments.

12 Comments

It worked, but when I refreshed the page I get an Application error. I have updated my code (see above).
I tried to access tue2id60.herokuapp.com (if you go to that site you'll see the error")
It looks like there was a problem during the heroku deployment. did you get any errors? try to look at the logs
The only error I can find is "NPM_CONFIG_LOGLEVEL = error"
Does it work localy? What exactly did you update in your code? Try to redeploy.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.