I just decided to use NodeMailer and it works well on localhost, but on the server, I don't know which path should I go to find the page's link. so here is the path in the app.js
// Static folder
app.use("/", express.static(path.join(__dirname, "/views")));
// Body Parser Middleware
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.get("/views", (req,res) =>{
res.render("contact");
});
and in the views folder, I have the file contact.handlebars
I tried on the live server this links to find my web-form
mywebsite/views/contact.handlebars (this is downloading the file)
mywebsite/views/ (this shows just the index of the views)
enter image description here
mywebsite/views/contact ( nothing )
Again, on my localhost, Everything works very well (localhost:3000/views)
I will be appreciated if someone would help me with this issue.
Related
I created a node server public/image folder to store my image file uploads. I can upload to the folder and store the url in the postgresql table with that users id with no problem. However, when I fetch that image url from the the database, I get the url but the browser always says "cannot get /...". Im using react to fetch the json response and add the image url property to the src in an avatar. Please help. I don't understand why. I'm using the app.use middelware below:
app.use(express.static('public')) app.use('/images', express.static('images'))
This is the image in the folder:
This is the avatar where I add the src props.
Here is the URL inside the browser react tools
in case you're getting a CORS error, you should use this:
var cors = require('cors')
const app = express()
app.use(cors())
app.use(express.static('public'));
you can access it with you're specified port:
http://localhost:PORT/filenameWithExtension
I have followed the steps on A2's tutorial for "Cannot GET" URL here:
https://www.a2hosting.com/kb/cpanel/cpanel-software/node-js-application-error-message-cannot-get-url
The main page, mydomain.com, loads fine and the CSS loads as well but
when I try to go anywhere else, like mydomain.com/port or mydomain.com/anythingelse, I get a 404 error (pic below).
The ejs and CSS templates are in the same file location, and if I change the "/" route to the other page's .ejs/.css that loads just fine so it is finding those routes, but for some reason no other URL routes besides "/" load.
mydomain.com - loads perfectly fine
mydomain.com/anythingelse - the 404 error
This is my app.js:
const express = require("express")
const app = express()
const path = require("path")
const cors = require('cors')
app.use(cors())
app.set("views", path.join(__dirname, "views"))
app.set("view engine", "ejs")
app.use("/test1/static", express.static("public/"))
app.get("/", function (req, res) {
res.render("home-visitor")
})
app.get("/port", function (req, res) {
res.render("port")
})
app.listen()
I have kept my Applicaiton URL blank on purpose so I have a clean home page address
I have tried using a non-blank Application URL and change the app.js file to match syntax accordingly with no difference in outcome (tried '/test/port/', '/test/port', 'test/port/', 'test/port'
This is what I find in the console under Network>post>Headers when I try to load the page on mydomain.com/port
Thanks for any help, I am still new to node.js being hosted with A2 Hosting so I am hoping I am missing something simple.
In your app.JS you have
app.set("views", path.join(__dirname, "views"))
app.set("view engine", "ejs")
That means that if you do something like
res.render(“port”)
inside of your views folder you need to have a port.ejs file
Take a look at one of my old repos
https://github.com/EnetoJara/KardexDL/tree/master/views/template
The code is a lil bit out of date but you can get some ideas
I am new to nodeJS server area, need help in understanding how to work with REST API (using express) and deploy the angular application over a singe node server and same ports.
By deploying i want to understand if user hit below url http://localhost:8000/<page_name> then the specified page should open.
And is user hit below url using get or post request
http://localhost:8000/api/<api_name> then a json or a text will be returned.
How to run both the thing over a single node server.
Lets assume, you have all your static files in the /public folder of you app. Generally spoken, if you are using express.static, you should also get your index.html because this is handled by default for each directory.
In your case, as you are using Angular, the routing is handled from the client side (SPA). You should only have one single index.html after building your Angular app. All files from your dist folder should then be placed into your /public folder. Then you need to make sure, that initial file serving provides your index.html like so:
In this example static files are served first, then your API and if nothing is found, you are getting back you index file.
const express = require('express');
const app = express();
// serve static files
app.static(__dirname + '/public'));
// serve your API
app.get('/api/welcome', function (req, res) {
res.send('Welcome');
});
// fallback routing (server side handling)
app.get(/.*/, function (req, res) {
res.sendFile(__dirname + ‘/public/index.html‘
});
app.listen(3000);
Next time please make sure, to give all necessary information in your question ;-)
With the help from Sebastian, so far I can find a solution but its not working when i am hitting URL for different pages.
const express = require('express');
const app = express();
app.use(express.static('public'))
Please provide your suggestions.
I am trying to implement a router on the server side with node.js and express.js.
I want to serve a file statically, but I already got this to work for a index.html file I created. No problems there.
My setup has tons of fileName.csp files (i.e. they end in .csp). Anyways, when I try to access a .csp file in the browser (inside it is really just a .html page - but the server side language must have it as a .csp file extension) but when I try to access it, the browser (google chrome) downloads the .csp file instead of rendering it!
I could really use some help since I'm new to all of this.
the line of code that is allowing the download of the .csp file, the code that exposes the directory where the .csp files live, is
app.use('/static', express.static('D:/CACHESYS/CSP/cah/'));
below is pretty much the whole snippet of code
var http = require('http');
var express = require('express');
var app = express();
app.use('/static', express.static('D:/CACHESYS/CSP/cah/'));
app.listen(3000, function() {
console.log('listening on port 3000');
})
app.get('/home', function(request, response) {
response.end('going to /home');
})
app.get('/csp/cah/MARS.csp', function(request, response) {
response.end('Trying to navigate to /csp/cah/MARS.csp');
})
p.s. the actual file path that is being downloaded is
D:/CACHESYS/CSP/cah/fileName.csp
just to give some more context for the question.
any help is appreciated
thanks!
You need to tell express what content type a .csp file is. You can do this with the following line:
express.static.mime.define({
'text/html': ['csp']
});
Although, if the file is an HTML file, it should probably have the .html extension. Also, if you are simply serving static files it is good practice to use an HTTP server like nginx or Apache to do that, rather than Node.js.
The reason this works is express will set the header Content-Type: text/html. This tells the browser it is HTML and it should render it as such. By default if a browser comes across a content type it doesn't recognise, it simply downloads it.
I'm asking for help after being unable to find a solution through other SO questions.
I'm doing some modular express app. This is the regarding part of my server.js file.
var app = express();
var login = require('./lib/login');
app.use(login);
app.use(express.static(path.join(__dirname, 'public')));
If I hit localhost:3000 It goes straight to public/index.html. But I want it to go to the login route. I've tried something like:
app.get('/', function(req, res){
res.redirect(__dirname + '/lib/login');
});
But it's not even being called since it always goes to index.html. Guess the line app.use(express.static(path.join(__dirname, 'public'))); is doing that from the get go. And Even if I comment that like for testing server's not redirecting to the route so I guess my code's not accurate.
I should add that inside the login route there's a jade template.
Any ideas?
More Information
Structure
/node_modules
/public
/javascripts
/stylesheets
/lib
/login
/views
server.js
Inside the public folder there's the index.html file that's being called. It's there just for testing, it has nothing to do with the real jade files I want to render.
I first added the app.use(express.static(path.join(__dirname, 'public'))); line so I can load my javascripts and my stylesheets in my jade templates with something like script(src='/javascripts/jquery.dataTables.js'). If I remove that original line from my server.js it DOES route / to login but then I can't seem to load those libraries inside javascripts and stylesheets, even if I change script(src='/javascripts/... to script(src='/public/javascripts/...