EJS Deployment Issue, "listnames are not defined" - node.js

I am referring to this document here: https://www.freecodecamp.org/news/develop-deploy-first-fullstack-web-app/#nextsteps
Yesterday with your help, I was able to successfully deploy a static website. now I am trying to deploy a dynamic website.I moved and named all my files as described in the article in the link.
my file structure is like this:
ls -l
total 52
drwxr-xr-x 3 debian debian 4096 Apr 18 02:55 Concept
drwxr-xr-x 67 debian debian 4096 Apr 19 22:50 node_modules
-rw-r--r-- 1 debian debian 305 Apr 19 22:49 package.json
-rw-r--r-- 1 debian debian 18488 Apr 19 22:49 package-lock.json
drwxr-xr-x 4 debian debian 4096 Apr 18 02:55 pictures
-rw-r--r-- 1 debian debian 568 Apr 20 00:01 server.js
-rw-r--r-- 1 debian debian 4621 Apr 18 02:55 styles.css
drwxr-xr-x 4 debian debian 4096 Apr 19 22:56 views
Then I have this
$ ls -l views/pages
total 8
-rw-r--r-- 1 debian debian 5231 Apr 19 23:40 index.ejs
And also this
$ ls -l views/partials
total 4
-rw-r--r-- 1 debian debian 1083 Apr 19 23:05 so_header.ejs
The content of server.js is this:
// Load Node modules
var express = require('express');
const ejs = require('ejs');
// Initialize Express
var app = express();
// Render static files
app.use(express.static('/home/debian/public'));
// Set the view engine to ejs
app.set('view engine', 'ejs');
// Port website will run on
app.listen(8080);
// *** GET Routes - Display Pages ***
// Root Route
app.get('/', function(req, res){
var listnames = ["Aras", "Songul", "Nafiz"];
// Render index page
res.render('/home/debian/public/views/pages/index.ejs', {
// EJS variable and server side variable
});
});
So when I initialize the server with npm start command and from my browser when I write and hit enter
http://myVpsIP:8080/
it gives an error something like this:
ReferenceError: /home/debian/public/views/pages/index.ejs:44
42|
43| <% include('/home/debian/public/views/partials/so_header') %>
>> 44| <% listnames.forEach(function(name){ %>
45| <p><%= name %></p>
46| <% }); %>
47|
listnames is not defined
at eval (eval at compile (/home/debian/public/node_modules/ejs/lib/ejs.js:662:12), <anonymous>:15:8)
at index (/home/debian/public/node_modules/ejs/lib/ejs.js:692:17)
at tryHandleCache (/home/debian/public/node_modules/ejs/lib/ejs.js:272:36)
at View.exports.renderFile [as engine] (/home/debian/public/node_modules/ejs/lib/ejs.js:489:10)
at View.render (/home/debian/public/node_modules/express/lib/view.js:135:8)
at tryRender (/home/debian/public/node_modules/express/lib/application.js:640:10)
at Function.render (/home/debian/public/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/debian/public/node_modules/express/lib/response.js:1012:7)
at /home/debian/public/server.js:17:6
at Layer.handle [as handle_request] (/home/debian/public/node_modules/express/lib/router/layer.js:95:5)
listnames are already defined in server.js and I am not really sure why it gives this error. Any help would be appreciated.

You should pass the listnames local variable to res.render() method like this:
res.render('/home/debian/public/views/pages/index.ejs', {
listnames
});
Then you can use listnames variable in your EJS template.

Related

Kubernetes gitconfig mounted as directory instead of file

I have some helm charts in which I would like to mount the git config globally on each container.
In this case the home directory for each container is / path. When I would like to do it manually on the container I am getting following
git config --global --add safe.directory "*" error: could not lock config file //.gitconfig: Permission denied
Now I want to map my config map to the global .gitconfig file.
set {
name = "git.sync.extraVolumeMounts[0].name"
value = "git-config"
}
set {
name = "git.sync.extraVolumeMounts[0].mountPath"
value = "/.gitconfig"
}
set {
name = "git.sync.extraVolumeMounts[0].subPath"
value = ".gitconfig"
}
With such config I am getting the .gitconfig as folder not the file
bitnami#airflow-web-7cdb6f5d6f-48mzh:/$ ls -la
total 84
drwxr-xr-x 1 root root 4096 Dec 2 12:20 .
drwxr-xr-x 1 root root 4096 Dec 2 12:20 ..
drwxrwsrwx 2 root bitnami 4096 Dec 2 12:20 .gitconfig
drwxr-xr-x 1 root root 4096 Jul 30 11:21 bin
Any idea what I am doing wrong? Is there any environment variable instead I can set?
I tried to use system config but it does not work either as some folder structure is missing.
Just for future, I was able to achieve that with the usage of the proper escaping
{
"name": "git.sync.extraVolumeMounts[0].mountPath",
"type": null,
"value": "/\\.gitconfig"
},
{
"name": "git.sync.extraVolumeMounts[0].name",
"type": null,
"value": "git-config"
},
{
"name": "git.sync.extraVolumeMounts[0].subPath",
"type": null,
"value": "gitconfig"
},
This basically, creates a .gitconfig file and not a folder.
Setup is working fine.

using nodejs and forever i get the error Cannot find module

I am trying to get forever to start up all my microservices
All the services fail the same way
my forever.json looks like (abbreviated)
[
{
"uid": "account-service",
"append": true,
"watch": true,
"script": "app.js",
"sourceDir": "/home/trig/account-service/",
"workingDir": "/home/trig/account-service/",
"logFile": "/home/trig/logs/forever.log",
"outFile": "/home/trig/logs/account_service_out.log",
"errFile": "/home/trig/logs/account_service_err.log"
},
after starting i do
forever list and get (abbreviated)
/home/trig/.forever/lQOi.log STOPPED
if i look at the log
data: account-service:25858 - module.js:471
data: account-service:25858 - throw err;
data: account-service:25858 - ^
data: account-service:25858 - Error: Cannot find module '/home/trig/account-service'
data: account-service:25858 - at Function.Module._resolveFilename (module.js:469:15)
data: account-service:25858 - at Function.Module._load (module.js:417:25)
data: account-service:25858 - at Module.runMain (module.js:604:10)
data: account-service:25858 - at run (bootstrap_node.js:383:7)
data: account-service:25858 - at startup (bootstrap_node.js:149:9)
data: account-service:25858 - at bootstrap_node.js:496:3
data: account-service:25858 - error: Forever detected script exited with code: 1
but if i do
ls -ls /home/trig/account-service
4 -rw-rw-r-- 1308 Dec 28 20:43 app.js
4 drwxrwxr-x 4096 Dec 28 20:43 controllers
4 drwxrwxr-x 4096 Dec 22 05:36 env
4 drwxrwxr-x 4096 Dec 28 20:43 models
4 drwxrwxr-x 154 4096 Dec 28 20:43 node_modules
4 -rw-rw-r-- 1 359 Dec 28 20:43 package.json
4 drwxrwxr-x 2 4096 Dec 28 20:43 utils
I then tried and changed the paths from absolute to relative ( based on comment below )
[
{
"uid": "account-service",
"append": true,
"watch": true,
"script": "app.js",
"sourceDir": "./account-service/",
"workingDir": "./account-service/",
"logFile": "/home/trig/logs/forever.log",
"outFile": "/home/trig/logs/account_service_out.log",
"errFile": "/home/trig/logs/account_service_err.log"
},
then did
forever start account-service ./forever.json
and still see the error
data: account-service:1795 - module.js:471
data: account-service:1795 - throw err;
data: account-service:1795 - ^
data: account-service:1795 - Error: Cannot find module '/home/trig/account-service'
data: account-service:1795 - at Function.Module._resolveFilename (module.js:469:15)
data: account-service:1795 - at Function.Module._load (module.js:417:25)
data: account-service:1795 - at Module.runMain (module.js:604:10)
data: account-service:1795 - at run (bootstrap_node.js:383:7)
data: account-service:1795 - at startup (bootstrap_node.js:149:9)
data: account-service:1795 - at bootstrap_node.js:496:3
data: account-service:1795 - error: Forever detected script exited with code: 1
if move the forever.json file into the dir /home/trig/account-service ( and remove the other services) and leave the sourceDir and workingDir to absolute path it starts correctly
I gave up trying to get forever to work with multiple apps .
I tried http://pm2.keymetrics.io/ and got it working pretty quickly.
so far so good

nodejs process.setgid, process.setuid behavior with fs module

directory:
drwxrwxr-x 2 alex alex 4096 Aug 3 12:03 ./
drwxr-xr-x 17 alex alex 4096 Aug 3 11:18 ../
-rwx------ 1 root root 19 Aug 3 11:24 privilegedStuff*
-rwxrwx--- 1 root root 28 Aug 3 12:10 privilegedStuff1*
-rwxrwxr-x 1 alex alex 830 Aug 3 12:12 test.js*
test.js:
#!/usr/bin/env node
var fs = require('fs');
console.log(' user id: ', process.getuid());
console.log(' group id: ', process.getgid());
console.log(' user effective id: ', process.getegid());
console.log('group effective id: ', process.getegid());
console.log('\n switching user and group...\n');
process.setgid(1000);
process.setegid(1000);
process.setuid(1000);
process.seteuid(1000);
console.log(' user id: ', process.getuid());
console.log(' group id: ', process.getgid());
console.log(' user effective id: ', process.getegid());
console.log('group effective id: ', process.getegid());
console.log('\n output: \n');
console.log(fs.readFileSync('./privilegedStuff1', 'utf8'))
// this throws error as expected so I commented that
// console.log(fs.readFileSync('./privilegedStuff', 'utf8'))
privilegedStuff1:
content of privilegedStuff1
result:
alex#hp:/apps/test$ sudo ./test.js
user id: 0
group id: 0
user effective id: 0
group effective id: 0
switching user and group...
user id: 1000
group id: 1000
user effective id: 1000
group effective id: 1000
output:
content of privilegedStuff1
so what I don't understand is why node doesn't throw an error as it does nicely with privilegedStuff file? What am I missing?
alex#hp:/apps/test$ groups
alex adm cdrom sudo dip plugdev lpadmin sambashare
alex#hp:/apps/test$ cat privilegedStuff1
cat: privilegedStuff1: Permission denied
alex#hp:/apps/test$ sudo -s
root#hp:/apps/test# groups
root
In my test, I don't have such problem.
Can you enter the following command and show the result:
ls -l privilegedStuff1
id

gruntfile files named patterns, possible?

I'm playing around with Ghost, and I'd like to make the gruntfile compile the sass files from my theme.
So I started by modifying the sass task:
...
sass: {
admin: {
files: {
'<%= paths.adminAssets %>/css/screen.css': '<%= paths.adminAssets %>/sass/screen.scss'
}
},
themes: {
files:{
'content/themes/**/css/ie.css': 'content/themes/**/src/sass/ie.sass',
'content/themes/**/css/print.css': 'content/themes/**/src/sass/print.sass',
'content/themes/**/css/screen.css': 'content/themes/**/src/sass/screen.sass'
}
}
}
...
I realised that I could simplfy this to :
...
sass: {
admin: {
files: {
'<%= paths.adminAssets %>/css/screen.css': '<%= paths.adminAssets %>/sass/screen.scss'
}
},
themes: {
files:{
'content/themes/**/css/*.css': 'content/themes/**/src/sass/*.sass',
}
}
}
...
But then I was thinking, why isn't it replacing the stars in the destination with what it matches from the source?
Ends up it was just creating the following:
$ ls -al ./content/themes/
total 0
drwxrwxr-x 1 zenobius zenobius 50 Nov 18 02:10 .
drwxrwxr-x 1 zenobius zenobius 46 Nov 15 11:02 ..
drwxrwxr-x 1 zenobius zenobius 6 Nov 18 02:10 ** <----- sigh
drwxrwxr-x 1 zenobius zenobius 128 Nov 15 11:02 casper
drwxrwxr-x 1 zenobius zenobius 250 Nov 18 00:08 crycilium
I guess my question is really:
can i use some kind of regex named patterns
could I use a function in the files option to process the output name as the destination?
So the solution was to make use of grunt.file.expandMapping, (thanks to : https://stackoverflow.com/a/16672303/454615):
...
themes: {
files: grunt.file.expandMapping([
"content/themes/**/src/**/*.sass",
"!content/themes/**/src/**/_*.sass",
], '', {
expand: true,
ext: '.css',
rename: function(base, src) {
grunt.log.write(base + " " + src);
return src.replace('/src/', '/../'); // or some variation
}
})
}
...

Getting the following error: 13279:can't find self in the replset config when configuring replica sets

I am configuring a 3 node mongodb replica set on linux. I am using the following config
fork = true
bind_ip = 127.0.0.1
port = 27017
verbose = true
dbpath = /opt/mongoDB/data/db
logpath = /opt/mongoDB/log/mongod.log
logappend = true
journal = true
replSet = rs1
keyFile = /opt/mongoDB/mongodb/bin/conf/keyfile
to start the server. I started the server and when I run connected to the server using mongo command line tool.
When I did rs.initiate() I get
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "host-ip:27017",
"ok" : 0,
"errmsg" : "couldn't initiate : can't find self in the replset config"
}
I tried providing the cfg to initiate() and still get the same error.
This is what shows up in the log file.
Mon Oct 14 13:27:33.218 [rsStart] replSet info no seed hosts were specified on the --replSet command line
Mon Oct 14 13:27:34.118 [conn1] run command admin.$cmd { replSetInitiate: { _id: "rs1", members: [ { _id: 0.0, host: "host-ip:27017" } ] } }
Mon Oct 14 13:27:34.118 [conn1] replSet replSetInitiate admin command received from client
Mon Oct 14 13:27:34.118 [conn1] replSet replSetInitiate config object parses ok, 1 members specified
Mon Oct 14 13:27:34.118 [conn1] getallIPs("host-ip"): [ip address]
Mon Oct 14 13:27:34.118 BackgroundJob starting: ConnectBG
Mon Oct 14 13:27:34.118 [conn1] User Assertion: 13279:can't find self in the replset config
Mon Oct 14 13:27:34.119 [conn1] replSet replSetInitiate exception: can't find self in the replset config
Mon Oct 14 13:27:34.119 [conn1] command admin.$cmd command: { replSetInitiate: { _id: "rs1", members: [ { _id: 0.0, host: "host-ip:27017" } ] } } ntoreturn:1 keyUpdates:0 locks(micros) W:230 reslen:107 1ms
When should I do to resolve this error?
I could not initiate Replica Set of MongoDB on CentOS machine.
http://docs.mongodb.org/manual/tutorial/deploy-replica-set
mongodb replset
rs.initiate()
{
"errmsg":"couldn't initiate : can't find self in the replset config on port 27011"
}
Then I just used JSON object parameter to rs.initiate(rsconfig)
var rsconfig = {"_id":"rs1","members":[{"_id":1,"host":"127.0.0.1:27011"}]}
then rs.add(..) or just all at once
var rsconfig = {"_id":"rs1","members":[{"_id":1,"host":"127.0.0.1:27011"},{"_id":2,"host":"127.0.0.1:27012"},{"_id":3,"host":"127.0.0.1:27013"}]}
check with print(JSON.stringify(rsconfig)) then
rs.initiate(rsconfig)
after several seconds check
rs.status()
Don't set 127.0.0.1 to bind_ip, change the bind_ip to the machine's name or ip address (such as 192.168.0.1).

Resources