I'm trying to learn the basics of node.js and am following a tutorial on youtube, but my code won't output to the terminal when I go to localhost:3000.
Here is my code:
app.js
var express = require("express");
var path = require("path");
var routes = require("./routes");
var app = express();
app.set("port", process.env.PORT || 3000);
app.use(routes);
app.listen(app.get("port"),function(){
console.log("Server started on port " + app.get("port"));
});
routes.js
var express = require("express");
var router = express.Router();
router.get("/", function(req,res){
console.log("Hello I'm on the start page here");
//res.render("index");
});
module.exports = router;
Can someone please explain why?
In app.js try this:-
const express = require("express"),
path = require("path"),
routes = require("./routes"),
app = express(),
port = process.env.PORT || 3000,
host = process.env.HOST || 127.0.0.1;
app.use(routes);
app.listen(port, host, (err) => {
if(err) console.log(`Some error has occured due to: ${err}`);
console.log(`The server has started on http://${host}:${port}/`);
});
For routes.js:-
const router = require("express").Router();
router.get("/", (req, res) => {
console.log("The home page of this web app.");
res.status(200).send("Home");
});
I am not 100% sure that this will work.
Thank you!