I'm new to node and full stack development and hope I ask this correctly and not carelessly or offensively for your particular level of understanding.
My dev environment is Internet connected, production will not be and I want to use bootstrap.css with node. Apache/httpd is not an option in production.
Testing bootstrap locally has been challenging, the correct directory for the css file is believed correct.
The main directory on CENTOS 7 is myapp, contains app0.js, the node_modules with espress and body-parser and all dependencies, and the views directory which contains grid.ejs.
myapp/
app0.js
node_modules
views/
grid.ejs
public/
bootstrap.css
Here is grid.html (which works on apache):
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Grid Test</title>
<link rel="stylesheet" type="text/css" href="bootstrap.css">
<style type="text/css">
.pink {
background: yellow;
border: 3px solid red;
</style>
</head>
<body>
<div class="row">
<div class="col-lg-2 pink"><center>2 columns </center></div>
<div class="col-lg-7 pink"><center>7 columns </center></div>
<div class="col-lg-3 pink"><center>3 columns </center></div>
</div>
</body>
</html>
And here is what apache shows for grid.html:
https://imgur.com/VanVZJn "bootstrap with httpd"
However app0.js does not!
So bootstrap.css works (1) when referenced on internet - which I cannot use in production (2) from httpd in my dev environment via http code
but not from an ejs file.
Note: I am able to reach bootstrap from node, because there were 404 errors when it was not reachable showing from the terminal where node was launched. After at least 12 hours trying to understand this, I'm stumped. Sadly with a short timeline to have a solution, I turn to you fine readers!
App0.js
//setup
var express = require('express'),
app = express(),
bodyParser = require('body-parser')
app.set('view engine','ejs');
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.get("/grid", function(req, res, next){
res.render("grid")
});
app.listen(3000,function(){
console.log("serving test demo on port 3000")
});
views/grid.ejs
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Grid System</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="public/bootstrap.css">
<style type="text/css">
.pink {
background: yellow;
border: 3px solid red;
}
</style>
</head>
<body>
<div class="row">
<div class="col-lg-2 pink"><center>2</center></div>
<div class="col-lg-7 pink"><center>7</center></div>
<div class="col-lg-7 pink"><center>3</center></div>
</div>
</body>
</html>
shown here:
https://imgur.com/ZLOgZKT "bootstrap not working"
You have to define on your app0.js file the public path from where you are going to serve static files. Try the following app0.js file requiring the path module and using express.static after bodyParser.json():
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
path = require('path')
app.set('view engine','ejs');
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, 'public')));
app.get("/grid", function(req, res, next){
res.render("grid")
});
app.listen(3000,function(){
console.log("serving test demo on port 3000")
});
And your grid.ejs file link the bootstrap file this way:
<link rel="stylesheet" type="text/css" href="/bootstrap.css">
Related
I am trying to send a html file through express but it is not able to include tailwindcss
I did all the set up correctly (at least that's what I think)
Is sendFile not capable of sending tailwindcss
This is the express setup
// express setup
const app = express();
const port = 9000;
app.use('/static', express.static('static'))
the endpoint
app.get("/", (req, res)=>{
res.sendFile(path.join(__dirname, "/template/home.html"))
});
html file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Website</title>
<link rel="stylesheet" href="../static/output.css" />
</head>
<body>
<h1 class="text-sm">Doesn't Works</h1>
</body>
</html>
css file
#tailwind base;
#tailwind components;
#tailwind utilities;
tailwind config file
module.exports = {
content: ["*"],
theme: {
extend: {},
},
plugins: [],
}
and yes the path are correct it works with normal css but not with tailwindcss
I also don't want to use pug for it
Is there a way around it?
You can simple Use the Play CDN to try Tailwind right in the browser without any build step.
Add the Play CDN script tag to the <head> of your HTML file, and
start using Tailwind’s utility classes to style your content.
But remember :
The Play CDN is designed for development purposes only, and is not the
best choice for production.
<script src="https://cdn.tailwindcss.com"></script>
Folder & file structure :
app.js
const express = require("express");
const path = require("path");
const app = express();
const port = 9000;
app.use(express.static("./public"));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "/public/home.html"));
});
app.listen(port, () => {
console.log(`Server is listening at http://localhost:${port}`);
});
home.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.tailwindcss.com"></script>
<title>Express & TailwindCss</title>
</head>
<body>
<div class="flex items-center justify-center h-screen flex-col gap-5">
<h1 class="text-5xl font-bold underline text-green-500">
Express with TailwindCss
</h1>
<h3 class="text-blue-900 text-3xl font-bold">It does works! ;-)</h3>
</div>
</body>
</html>
Output :
I am using this link as reference to build a simple ejs app.
Project structure:
- views
----- partials
---------- head.ejs
----- pages
---------- index.ejs
- package.json
- server.js
server.js:
const express = require('express'),
app = express();
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('pages/index');
});
head.ejs :
<title>My Awesome Site</title>
index.ejs :
<!DOCTYPE html>
<html lang="en">
<head>
<% include ../partials/head.ejs %> <!-- commenting out this line works -->
</head>
<body class="container">
<main>
<div class="jumbotron text-center header">
<img src="/images/phbc_17.jpg">
<h2 class="page-header">Be Awesome!</h2>
<p>Visit www.myawesomesite.org</p>
</div>
</main>
</body>
</html>
Index renders as expected when I am not including head.ejs partial in it. But, with head.ejs it's rendering only {} in the browser.
What am I doing wrong?
I saw two problems.
You didn't import ejsin your server.ejs file so import is as
const ejs = require('ejs');
Your ejs syntax is a mistake so use below syntax
<%- include ('../partials/head.ejs'); %>
and this need to be inside your body tag
Alright guys, I've been following the fcc node js tutorial for beginners. I have trouble getting the use() function from the express framework to work. I followed all the steps (I've copied exactly what he's doing), but when I open the Chrome/Firefox debug console my I can see my folders are not swapped for the alias I've set (is 'static' instead of 'public'). For anyone wondering, I'm stuck on this part -> https://youtu.be/-FV-moMWRSA?t=230.
my code:
const path = require('path');
const express = require('express');
const app = express(); //this function returns an object with many functions
app.use('/public', express.static(path.join(__dirname, 'static')));
app.get('/',
(req, res) => {
res.sendFile(path.join(__dirname, 'static', 'index.html'));
});
app.listen(3000);
my html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="/static/css/main.css">
<script src="/static/js/main.js"></script>
</head>
<body>
<p>Test paragraph</p>
</body>
</html>
anyone know what's going on?
You have wrong paths in your html. Since you're using /public in your middleware, only requests with /public will be looked in the static folder to see if a filewith the requested name exists or not.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="/public/css/main.css">
<script src="/public/js/main.js"></script>
</head>
<body>
<p>Test paragraph</p>
</body>
</html>
Hope this helps !
I have attached my css file to my html file. And then i run and open page using express in node js. However, the css file does not open when i run the webserver through node js.
html(show.ejs)
<html>
<head>
<link rel="stylesheet" type="text/css" href="assets/css/style.css" media="screen" />
</head>
<body>
<h1> Value is <%= detail %></h1>
</body>
</html>
node js
//required npm
var express = require('express');//express 4.*
var path = require('path');
// define app
var app = express();
// set up template engine
app.set('view engine', 'ejs');
//static files
//app.use('/static', express.static('/public')); //not working
app.use('', express.static(path.join(__dirname, 'public'))); //not working
//app.use(express.static(__dirname + '/public')); //not working
//app.use('/public/assets', express.static('public/assets')); //not working
app.get('/show/:id', function (req, res) {
res.render('./panel/show', {
detail: req.params.id ,
});
//port
app.listen(3000);
my project folder
node_modules
views
panel
show.ejs
public
assets
css
style.css
app.js
package.json
By entering <link rel="stylesheet" type="text/css" href="assets/css/style.css" media="screen" /> You are trying to find the assets folder in your out of public directory.
So, when you / it will find public directory which is statically defined in express server.
<html>
<head>
<link type="text/css" href="/assets/css/styles.css" rel="stylesheet">
</head>
<body>
<h1> Value is <%= detail %></h1>
</body>
</html>
I am trying to attach a backend to my angular 2 application. I have a Server.js file that grabs my index.html but I get an error at my System.import('app').catch(function(err){ console.error(err) }); in my index.html
error
SCRIPt5009: 'System' is undefined
index.html
<html>
<head>
<base href="/">
<title>LGR</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />
<link rel="stylesheet" href="./app/css/lgr.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err) }); <----ERROR HERE!
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
server.js
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var app = express();
var path = require('path');
mongoose.connect('mongodb://localhost:27017/LGR_db');
app.use('app', express.static(__dirname + "/app"));
app.use('node_modules', express.static(__dirname + "/node_modules"));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '../LGR', 'index.html'));
});
app.listen('3000', function () {
console.log("Server is running on localhost:3000");
});
I am pretty lost here and any help would be great. I guess I don't understand how to "define" system??
edit: added folder structure
Network output
I would say it's because you are sending the index.html file specifically, and doing a static serve for the stuff in /app. If the systemjs.config.js file was in the /app folder and referenced in the index.html as src='/app/systemjs.config.js' it may work.
-- EDIT: Still not 100% sure what the issue is. I did everything we discussed in it and nothing else. Glad it working though!