Node different require behaviour same app different servers - node.js

I have a project (express js) in which the main app.js has its declarations, i.e:
var express = require('express'),
routes = require('./routes'),
main = require('./routes/main'),
config = require('./config.js'),
...
So in a file like routes/main.js I could access the properties of config. I've also installed my express js on another server so all of a sudden a route in /routes/main.js tells me
ReferenceError: config is not defined
So if I redeclare it in there, now there isn't a null reference error but everything becomes undefined.
Both of the servers are running the script the same way (via forever). I am using a full path when starting scripts, but it does work fine on one server (centos) and not the other (ubuntu), but there aren't any other differences.

Related

Config variable for API key returns undefined in Heroku

I am using dotenv npm package to hide sensitive data in my app. The config variables for backend and frontend/React are defined locally inside .env file. The app works fine locally.
I deployed my app to Heroku and added config variables manually to Heroku.
I have 4 variables, 3 for backend, 1 for React. While the VARs for backend works properly in Heroku, the VAR which I use in React to fetch data from external API returns undefined.
React var has a prefix REACT_APP_ and as I said above it works fine locally and I can fetch data from external API. The only problem I have is, it doesn't work in Heroku.
I fixed the problem by removing the requirement for dotenv module in front-end. Simply I removed the code below and deployed to Hereoku again.
const path = require('path');
require('dotenv').config({path: path.resolve(process.cwd(), 'client', '.env.development'), debug: true});

Node / express - require exported js file inside route file

I created an app using express generator. I have a js file in my public/javascripts directory. I exported an obj:
module.exports = { my obj here }
Then in my route file (index.js) I've been trying to serve this object as an API so I can fetch it from another js file and do stuff on the front end.
But my require simply won't work. I tried:
var specs = require('./javascripts/specs')
and all variation of the path cause I assumed that my path was wrong.
Am I missing something obvious? I get the following error:
Error: Cannot find module '/javascripts/specs'......
File Structure
https://www.dropbox.com/s/ou9afzckboxbe1w/Screenshot%202018-04-18%2011.59.32.png?dl=0
Your public folder and your routes folder are on the same level in your directory. Therefore you need to go up to the common parent, then down through public/javascripts like this:
var specs = require('../public/javascripts/specs');
Your public/javascript is for client only scripts, if you want to require it from the server you have to provide the full path :
var specs = require('../public/javascripts/specs');

how to use the webodf editor in localhost with node.js

I did not find any tutorials to how to run the webodf i read his apis and source code i am getting how to start it can anybody share the idea.
- WebODF version 0.5.10-8-gf5949f3
-- Found Java: /usr/bin/java (found version "1.7.0.91")
-- external downloads will be stored/expected in: /home/peoplelink/build/downloads
-- Installed Node.js found: /usr/bin/nodejs - 0.10.25
-- good Node.js found: 0.10.25 (0.10.5 required.)
-- npm found: /usr/bin/npm
-- Android was not found: APK will not be built.
JS file dependencies were updated.
-- Configuring done
-- Generating done
-- Build files have been written to:
i got this but i am not getting to webodf.js file in it i am missed out anything.
I am not not sure what you currently have . But This is how you can configure an application using node.js to serve html files and view/edit odf files.
Lets Start with your Node.js Server
First Create an index.js file(name it whatever you want)in our application directory,then initialize node application using node init.
We will have following folder structure : -
./ document-editor
../app (our html code and libraries)
../index.js
../package.json
../And some other auto-generated files.
Include all the modules necessary.We are going to use Express,Multer and
other util libraries.
var express = require("express");
var multer = require('multer'); //for file handling
var util = require('util');
var app = express(); // init express app
Configure Routes and Html files to be served on user request to you server.
app.use(express.static('app')); // use this as resource directory
//APP ROUTING URL => FUNCTIONS
app.get('/', function (req, res) {
res.sendFile(__dirname + "/app/index.html");
});
// this means when we get a request on 'myAppContext/' url provide
index.html
Start the Server
//START THE SERVER
app.listen(3000, function () {
console.log("Listening on port 3000");
});
Note* : Make sure you have node.js envoirnment installed on your system before you start.
Now Lets Look at how we include webodf to our application.
First create a directory in your main folder(we name it 'app'), where all
the html,styles and scripts..etc will be stored.
/app (our html code and libraries)
../index.html
../script
..wodotexteditor-0.5.9(folder)
..myScript.js
../styles
../images
../And some other files.
Create an index.html file and include webodf and/or Editor JavaScript libraries(Contains Webodf included in build... so need to download separately).
Create a container element and local script necessary to run webodf editor.Make sure to add a odt file to directory for you test or you can use the one which comes with wodo-editor.
You can refer this link for creating a local webodf editor using wodo-text-editor and complete the above to steps(2 & 3).
After we have done the above things,we will go into our root directory and run 'node index' command.... and that's it folks.
Just hit localhost:3000/ and you will see a workable webodf editor.
I hope this helps to get started with node.js and webodf. I will soon create and full application with open/edit and save features using webodf and node.js.
Thanks :)

Setting Up Node.js App Directory

I'm completely new to using Node.js and even utilizing the command line, so this question may be extremely elementary, but I am unable to find a solution.
I am trying to set up an app directory using Node.js and NPM. For some reason, whenever I try to use the port:5000 I get a "Cannot GET/" error. My question is, why is my setup for my app directory not working?
I have installed connect and serve-static, and yet it will not retrieve files and listen on port 5000. I have created a server.js file in my user, kstach1. Here is the code I have within that file:
var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic('../angularjs'));
app.listen(5000);
So, I don't quite understand why this won't reference my folder of angularjs, where I want to store my app. I have tested it by adding a file within the folder called test.html, and entered localhost:5000/test.html, and still get the "Cannot GET/test.html" error.
I know that Node is working correctly because I can enter scripts into the command line and they give the correct output. I do this as a user (kstach1).
The only thing I can think of that I may be doing wrong, is where my files are located. I have the angularjs folder located in the root user folder on my Mac (kstach1), as well as the server.js file. Is this incorrect? If this is not the issue, is it because of where Node is installed (usr/local/bin/node)? My research to this point has led me to think that my problem could also be that I need to add the installation directory to my path. However, I don't want to mess with this unless I know that is the case.
Any help would be greatly appreciated.
I did a little research on the serve-static package and copied the code you provided.
My project folder is located at "C:\teststatic" and the folder with the static files is: "C:\angularjs", also using "text.html" that is located in the 'angularjs' folder.
When running the code you provided and going to localhost:5000 it indeed returns "Cannot GET/". This is most likely because there is no "/" file declared.
Going to localhost:5000/test.html works for me, so you could try setting a "/" like this:
app.use(serveStatic('../angularjs', {'index': ['test.html', 'index.html']}));
And see if that works for you. If not, you should double check directory names / locations.
EDIT:
From reading the comment you posted: try this instead:
app.use(serveStatic('angularjs'));
I suggest moving your angularjs folder up into your main project's directory in a public/ folder. Its a pretty standard convention to have all of your static assets in public/. You can then use the path module to automatically resolve your path, inserting this where you have '../angularjs': path.join(__dirname, 'public').
So, your code would look like this:
var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect();
var path = require('path');
app.use(serveStatic(path.join(__dirname, 'public'));
app.listen(5000);
And, your directory structure would look like this:
server.js
public/
angularjs/
test.html
You should then be able to use localhost:5000/angularjs/test.html to view your test.html

Running console commands through node inspector?

I have connected node inspector to my running node.js program. But I'd like to issue commands through the console - not necessarily on a breakpoint. For example, I have global variables and functions in my server.js program. For example:
var express = require('express');
var app = express();
function test() {
console.log('yo');
}
app.listen(3000);
Now in node-inspector I go into the console and I type 'test()' and it returns "ReferenceError: test is not defined", same things with global variables I type 'app' and it tells me it's not defined. Any idea how to make this thing work? I just want to run my node program and then issue commands to it via a command line interface.
#foreyez,
Your question inspired me to make an npm module that lets you debug (via command line) a running node.js application without setting breakpoints or needing to make variables global:
https://github.com/go-oleg/debug-live
Hope that helps!
The reason why it doesn't work as you expected is that all your variables and functions are local to your module (application) and you can't access them from a global context.
You can save them in the global scope if you want to access them from anywhere (including from the console when not stopped on a breakpoint):
var express = require('express');
var app = express();
function test() {
console.log('yo');
}
app.listen(3000);
global.test = test;

Resources