webpack -Error: Cannot find module 'pug' after hitting get-api - node.js

after configuring webpack in express and then an new folder fomed but when
i run bundle.js it successfully give a meassage- server is running on port 3000
bout when i hit api http://localhost:3000/api/test it gives loads whole bundle .js in console
and give error as
Error: Cannot find module 'pug'
at t (D:\wfh\Frontend-3.0-20220119T061742Z-001\Frontend-3.0\compressed\bundle.js:2:840468)
at new p (D:\wfh\Frontend-3.0-20220119T061742Z-001\Frontend-3.0\compressed\bundle.js:2:839766)
or can anyone tell me how not to have index.html output i want index.pug output

This is what I did on my sample project, try it out and see if it works for you too.
Basically, in your server.js (or whichever is the name of your entry server JS file), add the following:
app.set('views', path.join(DIST_DIR));
app.set('view engine', 'pug');
app.engine('pug', require('pug').__express); // <-- add this line
...
then try and build your project again.
I got the reference from the link below although it's for EJS.
https://stackoverflow.com/a/53897810/11826301

Related

Error trying to render the index.html that ng build generates with node and express

I want to deploy an application that I perform with the MEAN stack on Heroku, but I encounter 1 problem.
I have this folder structure, my node server, with a public folder, where is the dist / fronted folder and all the files generated by Angular's ng build --prod, it works when I start the server and browse normally, but if I refresh the page or write a route myself, I get these errors:
Errores
Sorry for my English.
If your are building a MEAN stack, you probably have a server.js or index.js or app.js as an entry point to your application. An SPA by definition manages all the routes within the router configuration. But if you try to refresh or type a route yourself, it is like you were trying to access that folder on the server (ex: www.mywebsite.com/about, here the folder about might not exist on the server, it is just known by your Angular app)
My suggestion is that you try to add this fix to the app.js (or server.js or app.js) file, so all unexisting routes or refresh go back to your index.html:
// Check your port is correctly set:
const port = process.env.PORT || 8080;
// Is saying express to put everything on the dist folder under root directory
// Check the folder to fit your project architecture
app.use(express.static(__dirname + "/dist"));
// RegEx saying "capture all routes typen directly into the browser"
app.get(/.*/, function(req, res) {
// Because it is a SPA, all unknown routes will redirect to index.html
res.sendFile(__dirname + "/dist/index.html");
});
app.listen(port);
This guy shows full deploy on Heroku with Angular: https://www.youtube.com/watch?v=cBfcbb07Tqk
Hope it works for you!

how to run angular 6 app with nodejs api on production?

Hi I am new to angular6
In development mode I have worked in angular 4200 port and node in different port (8080) in my operations.
but now I want to move my app into production mode.
for that I had build my angular project by using ng build command. after i get a dist folder is created on the root folder.
so i went to server.js file
here I had add my static file
app.use(express.static(path.join(__dirname,'dist/MDProject')));
app.use('/',(req,res)=>{
res.sendFile(path.join(__dirname,'dist/MDProject/index.html'))
});
click here to read my server.js file
by this
when ever I redirect to my local host it is opening but
in server.js side i didn't get any response.
when ever I try to login I am getting this error.
can anyone help me to solve this
I think there are 2 things wrong here in your code.
First, update your code
From:-
app.use('/',(req,res)=>{
res.sendFile(path.join(__dirname,'dist/MDProject/index.html'))
});
To:- This code needs to be added just before your app.listen(app.get('port')) code
app.get('*', function(req, res) {
res.sendfile('./public/MDProject/index.html');
// load the single view file (angular will handle the page changes on the front-end)
});
Second, Serve your file from your public folder(In your case I think it is the dist folder). So update your code like this.
app.use(express.static(__dirname + '/dist/MDProject'));
after I removed this line in my server.js file
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
and I paste this line before port creation.
app.use(express.static(path.join(__dirname,'dist/MDProject')));
app.use('/',(req,res)=>{
res.sendFile(path.join(__dirname,'dist/MDProject/index.html'))
});
now it is working fine.
Use this to go to index page.
app.get('*', (req, res) => {
res.sendfile('./public/MDProject/index.html');
})
with that you have to make folder in public in node
app.use('/', express.static('/dist/MDProject'))
Then in your app dist folder -> index.html
<base href="http://192.168.1.1:3000/admin-panel/">
This is my working code. Let me know if you have still any issue. :)

Node JS not sending file with res.sendFile()

My file structure is shown above, but I am unable to find the file with error: Undefined is not a function.
The app path is set, which I can get via a console.log:
/Users/myname/Desktop/myproject/client
If I navigate to localhost:3000 for example, the page is rendered correctly. I can then click to navigate to localhost:3000/login and everything is still all good. But if I go directly to localhost:3000/login, i.e. the index page is never loaded, then this route: '/*' is hit and the undefined error occurs. No HTML is loaded.
I set the app path like so:
app.use(express.static(path.join(__dirname, '/client')));
app.set('appPath', path.join(__dirname, '/client'));
I am using Express:
"~4.0.0"
Try to set the root for relative file path, this way:
app.set('base', __dirname);
and then:
app.use(express.static('client'));
Make sure not to include /client twice, so you don't get something like .../client/client... in your path.
http://expressjs.com/api.html#res.sendFile
res.sendFile() is supported from Express v4.8.0 onwards
You were close.
app.use(express.static(__dirname + '/client'));
Regards

Changing connect-assets lookup path

If I execute my expressjs app like so node app.js everything works fine however when I execute it like node path/app.js it starts looking for public assets from the execution location.
How can I change the path?
app.use(express.static(path.join(__dirname, 'public')) doesn't seem to be doing anything
The error I get:
Asset 'styles.css' not found in search path:
/path/public/css
/path/public/js

How to start a Nodejs, Express, Jade webapp on local server

I've been given an advanced project on a MacOSX computer, that uses Node.js, Express and Jade. I'm a little bit familiar with concepts for each, but have absolutely no experience with either one.
I've also found MAMP running on the dev machine where the app is in.
I'm clueless in terms of how to start or run the app.
The files and the database are there, I know how to get MAMP running, but it seems that files are not located or related to MAMP location.
I'm not really that familiar to Terminal either. I would like to know what should I run to get the webapp showing on a web browser (locally for now).
I' ve seen the application running on localhost and a port, but it won't run now.
I guess I haven't started something yet, but I honestly don't know what I'm looking for and how to run it.
Node.JS applications do not require a webserver, such as Apache. The applications are often the server themselves.
Typically, you start a Node application like this:
node yourApplication.js
How to access that application via a browser depends on how that application was written. There is no real standard, but it's just JavaScript, so you should be able to read through it fairly easily if you are a programmer.
After installing node and express the starting e easy :)
in the chosen folder:
express
npm install
node app.js
And you will be up and running.
Be sure that Nodejs, Express and Jade are already installed!
To start a local server you need to create app.js file in empty folder, for example in myApp folder. Copy this code to your app.js:
var express = require('express');
var app = express();
//Middleware
app.listen(3000)
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
});
app.get('/', function(){
res.render('index', {option: 'value'});
});
From myApp folder: express npm superApp. You can replace superApp to any name you like. Then type cd superApp && npm install and you must be relocated to myApp/superApp automatically.
Type npm install and after this you can run the server by typing node app.js (you should be in myApp/superApp). The server must be running now, go to http://localhost:3000/ and check.
If you see something like:
"Express
500 Error: /home/.../myApp/superApp/views/layout.jade:1 > 1| !!! 2| html 3| head 4| title= title !!! is deprecated, you must now use doctype"
Just change !!! to doctype in layout.jade.
Finally you should see this message:
"Express
Welcome to Express"
Now you can run the server just typing node app.js from myApp/superApp.

Resources