Roy
Roy

Reputation: 11

How to connect static HTML and CSS files to Node.js application?

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 () {

});

Upvotes: 1

Views: 487

Answers (1)

shudima
shudima

Reputation: 460

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>');
});

Upvotes: 1

Related Questions