express.static not working - javascript

I have the below code
var express = require('express');
var app = express();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.send('<h1>Hello world</h1>');
});
app.use(express.static(__dirname + '/public'));
http.listen(3000, function(){
console.log('listening on *:3000');
});
But when executing localhost:3000/Home.html where Home.html is the html file inside my public directory its saying Cannot GET /Home.html.
I dont know what mistake i did.
Please correct me.

Related

localhost | wrong path to file | JS

A couple days ago I started learning Node JS and after couple hours I opened my localhost and it didn't have any css or JS, because of that the wrong path to files that I wrote. Could someone tell me what exactly I need to write in path, so the localhost won't give error like GET http://localhost:3000/index.js net::ERR_ABORTED 404 (Not Found)
// MY NODE JS CODE:
const express = require('express')
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
})
app.use('/static', express.static(__dirname + '/public'));
io.on('connection', function(socket) {
console.log('connected');
socket.on('disconnect', function() {
console.log('disconnected');
});
});
http.listen(3000, function() {
console.log('listening on localhost:3000');
});
<!-- MY INDEX HTML CODE: -->
<link href="styles.css"> <!-- 1 link -->
<script src="index.js"></script> <!-- 2 link -->
<div id="something"></div>
Just change
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
})
app.use('/static', express.static(__dirname + '/public'));
into
app.use(express.static(__dirname + '/public'));

Serve an HTML file in node.js

app.get('/',(req,res)=>{
res.send("index.html ");});
app.listen(3000);
console.log("Server Started");
whenever i enter my local host IP address it displays "index.html" text instead of opening index.html file
You are trying to serve an HTML file, so you have to res.sendFile().
var express = require('express');
var app = express();
var path = require('path');
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.listen(3000);

Node.js: "require not defined" error happens in only one js

I have multiple node.js files in a project.
The server code is below:
app.js
var express = require('express');
app.use(express.static(__dirname + '/node_modules'));
app.use("/css", express.static(__dirname + '/css'));
app.use("/js", express.static(__dirname + '/js'));
app.use("/routes", express.static(__dirname + '/routes'));
var aws_router = require('./routes/aws')(app);
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
server.listen(8080);
Now I have another file aws.js in ./routes which essentially contains database operations
aws.js
var express = require('express');
var AWS = require('aws-sdk');
var credentials = new AWS.SharedIniFileCredentials({profile: 'default'});
AWS.config.credentials = credentials;
/*more code here*/
Now I am getting error
"Uncaught ReferenceError: require is not defined" in aws.js file for var express = require('express'); Why is that? The same definition is there also in app.js where it is all good.
What am I missing?

Static Content Serving Not Working In Express

var express = require('express');
var app = express();
app.get('/', function(req, res){
app.use(express.static('../../www'))
})
app.listen(8080)
according to docs this should work but it just returns a page of garbled text
It's better to use path module to join the current folder and relative path to an absolute path.
var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname, '../../www')));
app.get('/', function(req, res){
res.send('done');
});
app.listen(8080);
As #bulkan comments, you use /style.css to access www/style.css.
Move the app.use(express.static('../../www')); outside of the app.get like so;
var express = require('express');
var app = express();
app.use(express.static('../../www'));
app.get('/', function(req, res){
res.send('done');
});
app.listen(8080);
http://expressjs.com/api.html#app.use

failed to load static files express js

new to node development.
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
my content gets as below
server.listen(1337);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
I am trying to get static file as below
server.use("/", app.static(__dirname + '/'));
but it doesnt wonk getting errors.
How to get staTIC files?
just read express docs
var express = require('express'),
app= express(),
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
app.use("/", express.static(__dirname + '/'));
the static function is a method on the express module.
So it should be:
var express = require('express'),
app = express(),
io = require('socket.io').listen(server);
app.use(express.static(__dirname + '/'));

Categories