Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS w/Express Error: Cannot GET /

This is what i have, the filename "default.htm" actually exists and loads when doing a readFile with NodeJS.

var express = require('express'); var app = express();  app.use(express.static(__dirname + '/default.htm'));  app.listen(process.env.PORT); 

The Error (in browser):

Cannot GET / 
like image 907
sia Avatar asked Nov 12 '12 07:11

sia


People also ask

What is Express () in node JS?

Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.

Why is node js not working?

Problems occurring in Node. js application deployments can have a range of symptoms, but can generally be categorized into the following: Uncaught exception or error event in JavaScript code. Excessive memory usage, which may result in an out-of-memory error.

Why is Express JS Unopinionated?

Express JS is minimal and unopinionated​ js” (ExpressJS, 2020). Express uses less overhead in the core framework so that makes it minimal and a good choice for building out large web applications. You don't want to have a framework that fills your codebase with lots of bloatware that you are never gonna use.

CAN node run without Express?

You cannot use Express without NodeJS by definition so you have to deploy your backend somewhere else in you want to use it.


1 Answers

You typically want to render templates like this:

app.get('/', function(req, res){   res.render('index.ejs'); }); 

However you can also deliver static content - to do so use:

app.use(express.static(__dirname + '/public')); 

Now everything in the /public directory of your project will be delivered as static content at the root of your site e.g. if you place default.htm in the public folder if will be available by visiting /default.htm

Take a look through the express API and Connect Static middleware docs for more info.

like image 73
Sdedelbrock Avatar answered Sep 17 '22 03:09

Sdedelbrock