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

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?

Related

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

Unable to render index.html file from my view folder in angular2

I'm creating a MEAN Stack application where angular has setup in /client folder. I want that when I run npm start command in /client folder it should render index.html file from /views folder, what I'm doing wrong getting this error
Cannot GET /
Folder structure is as follows.
meanApp
----- client (angluar2 setup here but doesn't have an index.html file)
---------- app
----- views
----------index.html
----- routes
----- server.js
Codes in server.js
var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var index = require('./routes/index');
var tasks = require("./routes/tasks");
var app = express();
//View engines
app.set("views", path.join(__dirname,'views'));
app.set("view engine", 'ejs');
app.engine("html", require("ejs").renderFile);
//Set static folder
app.use(express.static(path.join(__dirname,'client')));
// Body parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use('/', index);
app.use('/index', index);
app.use('/api', tasks);
//listen
app.listen(3000, function(){
console.log("Server listing # 3000");
});
Here you need to define route for express server like :
app.set('appPath', 'client'); //this is a folder where your index.html is
app.route('/*')
.get(function(req, res) {
res.sendfile(app.get('appPath') + '/index.html');
});
This will cause every call in broweser to render index file.
const http = require('http');
fs = require('fs');
var express = require('express');
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');
var app = express();
app.set('appPath', 'views');
app.use(express.static(__dirname + '/views'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(expressValidator());
app.use('/*', function(req, res, next) {
res.sendfile(app.get('appPath') + '/index.html');
});
http.createServer(app).listen(3001, function() {
console.log(`Express server listening on port 3001`);
});
exports = module.exports = app;

using files with express server

var express = require('express'),
app = express(),
db = require('./db'),
bodyParser = require('body-parser'),
controller = require('./controller');
app.use(express.static('../public'));
app.get('/server', function (req, res) {
console.log(__dirname);
res.sendFile('/../client/index.html');
});
I have this express server set up but using the code above I get "Cannot GET /" when I view localhost:portnumber. If I change the GET method to:
app.get('/', function(req, res) {
res.sendFile(__dirname + '../client/index.html');
});
I get "'C:\Users\TC\Documents\Desktop\myapp\multiplayerWebSite\server..\client\index.html' at Error (native)" and if I change it to:
app.get('/', function(req, res) {
res.sendFile('../client/index.html');
});
I get "TypeError: path must be absolute or specify root to res.sendFile"
Error: ENOENT: no such file or directory
The server was working perfectly when I had everything in the root directory, but I wanted to change the folder structure to make it more neat/professional. If anyone could tell me what I'm doing wrong I'd appreciate it greatly. Thank you in advance!
You can use path module, there is a join method that take multiple paths to make one.
Exemple if you do:
path.join('test/musicfolder', '../videofolder/allreadyseen')
you will get 'test/videofolder/allreadyseen' .
you can see all the doc here : https://nodejs.org/api/path.html
var express = require('express'),
path = require('path'),
app = express(),
db = require('./db'),
bodyParser = require('body-parser'),
controller = require('./controller');
app.use(express.static(path.join(__dirname, '../public')));
app.get('/server', function (req, res) {
console.log(__dirname);
res.sendFile(path.join(__dirname, '../client/index.html'));
});

express.static not working

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.

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