javascript src file 404 (Not Found) - javascript

I am facing error "file not found" in an angular javascript. I am using Nodejs and express for server and angularjs in html5. AngularChart.html is in "public" folder in which I am trying to refer one javascript in one level up.
script type="text/javascript" src="../check.js"
It is giving me the error in console.
GET http://localhost:3000/check.js 404 (Not Found)
Here is my folder structure.
\myapp\public\AngularChart.html
\myapp\check.js
it works when in refer files folder down but nothing working in folder up. I suspect it may be because of the __dirname in app.js which is as below.
var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
Please help.
Thanks

Your chart.js should be public so add it inside the public directory otherwise it gives a 404 error (Not found) since it is not visible to the client.(browser)

Related

hosting .ejs file and node js

How do you host multiple webpages with an extension file of .ejs? I have a login page coded in .ejs and I wanted to host it on node.js by using those .ejs file and other pages connected with the login page I have or rather I wanted the login.ejs file to run using node.js. Thank you very much
Put all your *.ejs files in views folder (recommended way) and then set them as views in express using following code
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
Now you can start rendering them on the path you wanted like below
router.get('/foo', function(req, res) {
res.render('foo') // this renders /views/foo.ejs
}

how to use multiple view engine like ejs and jsx in node js?

I am using ejs and jsx both file as template in node.js project but I am unable to find any solution and get error like this:
my code where both view engine included given under:
//view engine setup
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'app/views'));
app.set('view engine', 'jsx');
var options = { beautify: true };
app.engine('jsx', require('express-react-views').createEngine(options));
var engines = require('consolidate');
app.engine('ejs', engines.ejs);
on execution i am getting error "Error: Failed to lookup view "login" in views directory"
while i have created both jsx file and ejs file in view directory but not rendering.Any one can give me solution?

Include css to html using express

I am trying to include a css sheet to my simple html page.
I am using parse.com hosting to host a dynamic webpages that uses express.
There is a ton of answers on this questions, but none of them seemed to work for me.
I am trying to include my css file from my .ejs page with this line of code
<link rel="stylesheet" type="text/css" href="css/content.css" />
I copyed my css folder inside the public floder
The most suggested answer is to add this line of code:
app.use(express.static(__dirname + '/public'));
but it did not work.
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
// Global app configuration section
app.set('views', 'cloud/views'); // Specify the folder to find templates
app.set('view engine', 'ejs'); // Set the template engine
app.use(express.bodyParser()); // Middleware for reading request body
// This is an example of hooking up a request handler with a specific request
// path and HTTP verb using the Express routing API.
app.get('/content/:id', function(req, res) {
res.render('content', {message: "hi"});
});
I am following this guide to set up express:
https://www.parse.com/docs/hosting_guide#webapp-started
when I try to deploy my code, I get this error "Update failed with Could not load triggers. The error was ReferenceError: __dirname is not defined"
What am I doing wrong here?
After looking into this problem for some more time, I read this on parse.com website.
"our Express app can sit side-by-side with any static content you deployed from your public folder. When a request goes to a URL of your subdomain, Parse will first look for a matching file in the public directory. If there is no match, then Parse will invoke any Express request handlers that you have registered in Cloud Code. If there is still no match, Parse will render a "404 Not Found" page."

Webpage cannot find certain files

I am building a simple sandbox/practice style website on heroku I am trying to learn nodejs and I am learning Angular. After much hit and miss I got the server to load my home.html page, however it cannot seem to find my vendor scripts. Such as my angular, contollers, and some images/css.
The path I have setup for angular is: <script src="/vendor/angular.min.js"></script>
<script src="/controller.js"></script>
This is coming from a localhostconnection by the way, so http://localhost Here is an example of what my simple index.js in node looks like:
var express = require("express");
var app = express();
app.set("port", (process.env.PORT || 5000));
app.engine('html', require('ejs').renderFile);
app.set('views engine', 'html');
app.use(express.static(__dirname + "/public"));
app.get("/", function(request, response) {
response.render("home.html");
});
app.listen(app.get("port"), function() {
console.log("Node app is running at localhost:" + app.get("port"));
});
this is located in the root of the webfiles and it points to a views folder which includes my home.html (how I got my home page to finally load) However it still says it cannot find my vendor scripts.
These are located inside their own vendor folder that is in the root folder, and the views folder is also its own folder in the root folder the setup is like so:
-views
---home.html
-vendor
--some vendor folders(bootstrap etc)
---angularjs.js and other vendor scripts
I am sure I am missing something simple here, but I am a beginner with nodejs and still learning at this point. Thank you ahead of time for the help, and let me know if I can offer anymore information.

"Error: Cannot find module html" when visiting HTML page

When I started my application, and visited localhost:8333 in my browser, it threw an error:
Error: Cannot find module 'html'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at new View (C:\Users\fr\node_modules\express\lib\view.js:42:49)
at Function.app.render (C:\Users\fr\node_modules\express\lib\application.js:483:12)
at ServerResponse.res.render (C:\Users\fr\node_modules\express\lib\response.js:755:7)
at allClients (C:\Users\fr\node_modules\apps\chat.js:13:7)
at callbacks (C:\Users\fr\node_modules\express\lib\router\index.js:161:37)
at param (C:\Users\fr\node_modules\express\lib\router\index.js:135:11)
Here's my code:
var io = require('socket.io');
var express = require('express');
var app = express(),
http = require('http'),
server = http.createServer(app),
socket = require('socket.io').listen(server);
app.configure(function(){
app.use(express.static(__dirname));
});
app.get('/', function(req, res, next){
res.render('./test.html');
});
server.listen(8333);
This is my project folder structure:
node_modules/
express/
socket.io/
apps/
chat.js
test.html
This is my new app.configure:
app.configure(function(){
app.use(express.static(path.join(__dirname, 'public')));
});
But that code fails with this error:
path is not defined
I am assuming that test.html is a static file. To render static files, use the static middleware like so:
app.use(express.static(path.join(__dirname, 'public')));
This tells Express to look for static files in the public directory of the application.
Once you have specified this, simply point your browser to the location of the file and it should display.
If, however, you want to render the views, then you have to use the appropriate renderer for it. The list of renderers is defined in the consolidate.js library.
Once you have decided which library to use just install it. I use mustache so here is a snippet of my app file:
var engines = require('consolidate');
app.set('views', __dirname + '/views');
app.engine('html', engines.mustache);
app.set('view engine', 'html');
This tells Express to—
Look for files to render in the views directory
Render the files using mustache
The extension of the file is .html (you can use .mustache too).
A simple way is to use the EJS template engine for serving .html files. Put this line right next to your view engine setup:
app.engine('html', require('ejs').renderFile);
Install the ejs module from npm:
npm install ejs
Then just paste below two lines in your main file (app.js or main.js):
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
Install consolidate and mustache by executing the below command in your project folder.
sudo npm install consolidate mustache --save
And make the following changes to your app.js file
var engine = require('consolidate');
app.set('views', __dirname + '/views');
app.engine('html', engine.mustache);
app.set('view engine', 'html');
And now HTML pages will be rendered properly.
I think you might need to declare a view engine. If you want to use a view/template engine:
app.set('view engine', 'ejs');
or
app.set('view engine', 'jade');
But to render plain HTML, see this post.
Use res.sendFile() instead of res.render(). What you're trying to do is send a whole file.
Try installing the html module from npm:
npm i html

Categories

Resources