i am not able to include static CSS file in the express app
I have used app.use(express.static('public') still in the output file CSS in not include
// initialize modules // ATHARVA BHAVSAR
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
//creating app from express method
const app = express();
//body parser
app.use(bodyParser.urlencoded({ extended: true }));
//including static files in the app
app.use(express.static("public"));
//get method for the app
app.get("/", (req, res) => {
res.sendFile(__dirname + "/signup.html");
});
//post method for the app
app.post("/", (req, res) => {
//getting the entries from the user
const FirstName = req.body.Fname;
const LastName = req.body.Lname;
const Email = req.body.email;
//trial
console.log(FirstName, LastName, Email);
});
//listening to port 3000
app.listen(3000, function () {
console.log("Server is running on port 3000");
});
Well, you're including the static files correctly, but how are your folder structure looking like? since you called app.use(express.static("public")); your css should be inside a folder called "public", also can I take a look at your html? You still need to link your css in your html file, you know right?
Related
I am working on layouts and templating. I have a site where I have files "views/partials/header.ejs" and "views/partial/footer.ejs". I have the app.js file with templating set up as follows:
const express = require('express')
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const ejs = require('ejs');
const date = require(__dirname + '/date.js');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.get('/', function(req, res) {
const year = date.getYear();
res.render('home', {copyrightYear: year})
});
app.listen(3000, function() {
console.log('Server started on port 3000');
});
What I need to do is pass the const copyrightYear into the partial "footer" instead of having to to input into each route. Any advice would be welcomed.
Thanks in advance.
you can't do that because when you make a request the server get the required file the does the logic on it and then it send it back to the browser .
so when you use this
app.get('/', function(req, res) {
const year = date.getYear();
res.render('home', {copyrightYear: year})
});
the server will get the home file which includes the partials files then pass the data then send it to the browser .
so you must make separate request for each file you want and then do the logic on it
i am trying to get data from a html form using express
but when i try to load my static files with app.use(express.static(__dirname+"public");
in the devtools it is showing that it failed to load my response data
can anyone tell me why this is happening and a solution for this
here is the code in the first image
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static(__dirname+'public'));
app.get("/",function(req,res){
res.sendFile(__dirname + "/public/bmiCalculator.html");
});
app.post("/",function(req,res){
console.log(req.body);
res.send("hello");
});
app.listen(300,function(){
console.log("Server Hosted OK");
});
The path provided to the static middleware is incorrect. The following code __dirname+'public' will give you Calculatorpublic, i.e. / is missing. What you can do is to add the / before public:
app.use(express.static(__dirname, "/public"));
Another variation:
const path = require('path');
app.use(express.static(path.join(__dirname, "public")));
I am trying to develop an application in NodeJs using express framework. My routing is working when I navigating from home to inner pages. But If I want to navigate from some inner page to homepage then it is not working.
Below is my app.js code.
const express = require('express');
const path = require('path');
const engines = require('consolidate');
const bodyParser = require('body-parser');
//declare all routers
var home = require(path.join(__dirname, "/routes/index"));
var myaccount = require(path.join(__dirname, "/routes/myaccount"));
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.engine('html', engines.handlebars);
var defaultViewPath = path.join(__dirname, "/views");
app.set('views', defaultViewPath);
app.set('view engine', 'html');
app.use('/', home);
app.use('/myaccount', myaccount);
Here if I have navigated from home to myaccount - Its working
But if I am navigating from myaacount to home - It reloads the same page.
Can anyone help me to resolve this issue.
To define routing using methods of the Express app object, use app.get() to handle GET requests
var express = require('express')
var app = express()
// When GET request is made to the homepage
app.get('/', function (req, res) {
res.render('home');
});
// When GET request is made to the myaccount
app.get('/myaccount', function (req, res) {
res.render('myaccount');
});
app.get('/myaccount/innerpage', function (req, res) {
res.send('Hello Inner Page');
});
//Page Not Found
app.use(function(req, res){
//render the html page
//res.render('404');
res.sendStatus(404);
});
Hope this could help you
use app.get and app.post Route Methods
app.get('/',function(req,res){
res.render('home');
});
app.get('/myaccount',function(req,res){
res.render('myaccount');
});
Or Create Router File For Home & myAccount
var express = require('express')
var router = express.Router()
router.get('/', function (req, res) {
res.send('Home Page')
})
module.exports = router
in Your app.js or index.js file , require route.js
var home = require('./route');
app.use('/', home)
In project nodeJS - express, after install module by npm, when require "create-file" module in app.js const cf = require('create-file') , i can't use it in main.js, in console i have this return cf is not defined when i run node.
app.js
` const express = require('express');
const ejs = require('ejs');
const bodyParser = require('body-parser');
const cf = require('create-file');
// init app
const app = express();
//Template engine setup
app.set('view engine','html');
app.engine('html',ejs.renderFile);
// Index route
app.get('/',(req, res) => {
res.render('index.html');
})
// Public folder setup
app.use(express.static(__dirname + '/public'));
// Body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Catch from submit
app.post('/', (req, res) => {
res.send(req.body);
})
// Define port
const port = 3030;
// Start server
const server = app.listen(port, () => console.log( 'Server start on port
3030 '));`
main.js
`const button12 = document.getElementById('button');
button12.onclick = function () {
cf('C:\Users\tmeda\Bureau\fileTest.txt', 'my content\n', function (err) {
// file either already exists or is now created (including non existing
directories)
});
}`
Define Your Variable as global in in your app.js file
like global.cf = require('create-file');
So this way you can access cf variable in any files , no need to require it again in different file .
I am trying to get access to query parameters using Express in Node.js. For some reason, req.params keeps coming up as an empty object. Here is my code in server.js:
const express = require('express');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const https = require('https');
//custom packages ..
//const config = require('./config');
const routes = require('./routes/routes');
const port = process.env.port || 3000;
var app = express();
//templating engine Handlebars
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
//connect public route
app.use(express.static(__dirname + '/public/'));
app.use(bodyParser.json());
//connect routes
app.use('/', routes);
app.listen(port, () => {
console.log( 'Server is up and running on ' + port );
});
And here is my routes file:
//updated
const routes = require('express').Router();
routes.get('/', (req, res) => {
res.render('home');
});
routes.post('/scan', (req, res) => {
res.status(200);
res.send("hello");
});
routes.get('/scanned', (req, res) => {
const orderID = req.params;
console.log( req );
res.render('home', {
orderID
});
});
module.exports = routes;
When the server is up and running, I am navigating to http://localhost:3000/scanned?orderid=234. The console log that I currently have in the routes.js file is showing an empty body (not recognizing orderid parameter in the URL).
orderid in the request is query parameter. It needs to be accessed via req.query object not with req.params. Use below code to access orderid passed in the request:
const orderID = req.query.orderid
Now you should be able to get 234 value passed in the request url.
Or try replacing the code for route /scanned with below:
routes.get('/scanned', (req, res) => {
const orderID = req.query.orderid
console.log( orderID ); // Outputs 234 for the sample request shared in question.
res.render('home', {
orderID
});
});
The reason that req.body keeps coming up as an empty object is that in get requests, such as those made by the browser on navigation, there is no body object. In a get request, the query string contains the orderid that you are trying to access. The query string is appended to the url. Your code can be rewritten as below:
routes.get('/scanned', (req, res) => {
const orderID = req.query.orderid
console.log( req );
res.render('home', {
orderID
});
});
A note, though, is that if you have AJAX posts firing within your client side code, your req.body will not be empty, and you can parse it as you would normally.