I've a .env formatted like this:
NODE_ENV=development
SERVER_PORT=5000
DB_URL=mongodb://localhost:27017
DB_NAME=test
DB_USER=user
DB_PASS=*****
I use config package (from NPM) to manage all app option, like server port and database connection
All config data are stored in JSON files like this:
{
"server": {
"port": 5000
},
"db": {
"url": "mongodb://localhost:27017",
"name": "test",
"user": "user",
"pass": "*****",
}
}
Now I've to write down all the data into config file (JSON) from ENV every time NodeJS starts
How can I do?
Related
So I'm somewhat new to the whole web development thing with node.js and I'm wondering if someone could help me out with understanding how to implement my application correctly.
So the app is a simple landing page with an email form that takes an email and sends it to the API. I designed this functionality without issue except when I launched my website i'm getting a required not defined error.
I understand that this is because node.js is a server side technology so that when the application goes live, the client doesn't understand what required means.
Through further research, I figured out that I had two options and that's to either implement synchronous dependencies via something like Browserify or take things asynchronously and use something like RequireJS.
Right now I've decided on using Browserify, (unless someone can convince me otherwise) I just need help with figuring out how to implement it for my specific app.
app.js
//The dependenices my node js app needs (also where the require bug occurs)
//------------------------------------------------------------------------------------------------------------------------
const express = require('express'); //Require the express package that we installed with npm install express
const request = require('request'); //Require the express package that we installed with npm install request
const bodyParser = require('body-parser'); //Require the express package that we installed with npm install bodyParser
const path = require('path'); //Require the express package that we installed with npm install path
//------------------------------------------------------------------------------------------------------------------------
const app = express(); //Creates the application with express
//Middleware
app.use(express.json()); //Tells express to use JSON as it's input
app.use(bodyParser.urlencoded({ extended: false })); //Prevents XSS, only will return if the url has specified enconding
app.use(express.static(path.join(__dirname, '/site'))); //Tells the app to use the current path D:\Gaming Galaxy\Website\Website\main\site as the dir for the website
console.log("The directory used is", express.static(path.join(__dirname, '/site')));
app.post('/subscribe', (req, res) => { //Makes a post request to express req is the request and res is the response
const { email, js } = req.body; //Create a constant that makes up of the request's body
const mcData = { //Create a constant JSON object mcData, that contains the email from the above constant and a status message
members: [
{
email_address: email,
status: 'pending'
}
]
}
const mcDataPost = JSON.stringify(mcData); //Turns the JSON object into a string
const options = { //Sets a JSON object of a bunch of options that mailchimp will use
url: 'https://us20.api.mailchimp.com/3.0/lists/f10300bacb',
method: 'POST',
headers: {
Authorization: 'auth f24c3169da044653d1437842e39bece5-us20'
},
body: mcDataPost
}
if (email) { //If there's an email that exists
request(options, (err, response, body) => { //Send a request to mail chimp
if (err) { //If there's an error
res.json({ error: err }) //Print said error
} else { //If there's not an error
if (js) { //If javascript is enabled (boolean)
res.sendStatus(200); //Send a success message
} else {
res.redirect('/success.html'); //If it's disabled, send them to a successful HTML page.
}
}
})
} else {
res.status(404).send({ message: 'Failed' }) //If the email doesn't exist, have it fail
}
});
app.listen(5000, console.log('Server started!')) //Console log that confirms the start of the server
package.json
{
"name": "gaminggalaxy",
"version": "1.0.0",
"main": "site/js/app.js",
"dependencies": {
"body-parser": "^1.19.0",
"commonjs": "^0.0.1",
"express": "^4.17.1",
"index": "^0.4.0",
"node-fetch": "^2.6.6",
"prototype": "^0.0.5",
"request": "^2.65.0",
"requirejs": "^2.3.6",
"uniq": "^1.0.1"
},
"devDependencies": {
"nodemon": "^2.0.15"
},
"scripts": {
"serve": "node app",
"dev": "nodemon app"
},
"keywords": [],
"author": "",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+https://github.com/InvertedTNT/Main.git"
},
"bugs": {
"url": "https://github.com/InvertedTNT/Main/issues"
},
"homepage": "https://github.com/InvertedTNT/Main#readme",
"description": ""
}
index.html (the form itself)
<form action="/subscribe" method="POST">
<div class="newsletter-form-grp">
<i class="far fa-envelope"></i>
<input type="email" name="email" id="email"
placeholder="Enter your email..." required>
</div>
<button id="cta">SUBSCRIBE <i class="fas fa-paper-plane"></i></button>
</form>
folder structure
node_modules
site
-index.html
-css
-js
- app.js
-images
app.js
package-lock.json
package.json
Thank you for your help, I would appreciate any sort of advice on how I can use those dependencies and the overall implementation of browserify.
A browser is an HTTP client.
Express is a framework for building HTTP servers.
HTTP clients make requests to HTTP servers which then send responses back.
Express depends on Node.js. It requires features provided by Node.js (like the ability to listen for network requests) which are not available in browsers.
Browserify can bundle up JavaScript which is written using modules into non-module code that can run in a browser but only if that code does not depend on Node.js-specific features. (i.e. if the JS modules are either pure JS or depend on browser-specific features).
Browserify cannot make Express run inside the browser.
When you run your JS program using Node.js you can then type the URL to the server the program creates into the browser’s address bar to connect to it.
i currently learn Node.js development and i try to start my Webcode. Im working with Visual Studio Code on Windows.
When i start the application with "npm start", the website on "localhost:3000/" just shows the "Server Connect". But how do i see all of the other files like the "Index.html"? Do i have to add them anywhere?
Thank you in advance.
Https Server File:
const http = require('http');
http.createServer(function(req, res){
res.write('Server connect');
res.end();
}).listen('3000');
package.json
{
"name": "project-1",
"version": "1.0.0",
"description": "Project 1",
"main": "index.html",
"scripts": {
"start": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"chartjs": "^0.3.24",
"express": "^4.17.1",
"http": "0.0.1-security",
"mongodb": "^3.6.2",
"mongoose": "^5.10.11",
"nodemon": "^2.0.6"
}
}
Write code which looks at req. A property on that object there will tell you the path that is being asked for. (The API documentation for the http module will tell you what property that is).
Then decide what you want to respond with based on that path.
If it is a static file, then read that static file from the file system and output it in the response.
Make sure you set the right Content-Type headers. (You'll get problems if you send HTML but say it is plain text or a JPEG image which you say is HTML).
You are reinventing the wheel here. You should probably look at the Express.js framework and its static module.
To add what #Quentin said. Here's a good example from W3.
https://www.w3schools.com/nodejs/shownodejs.asp?filename=demo_ref_http
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World!');
res.end();
}).listen(3000);
I created a node.js (with express) application and have been unsucessfully trying to deploy to OpenShift from github. I am attempting to deploy from the web interface (providing the URL to the github repository root and "master" in the branch/tag field) and am getting an error I'm having trouble understanding:
The initial build for the application failed:
Shell command '/sbin/runuser -s /bin/sh 5724c3b42d5271363b000191 -c "exec /usr/bin/runcon 'unconfined_u:system_r:openshift_t:s0:c4,c687' /bin/sh -c \"gear postreceive --init >> /tmp/initial-build.log 2>&1\""' returned an error. rc=255 .
Last 10 kB of build output: Stopping NodeJS cartridge
Repairing links for 1 deployments
Building git ref 'master', commit a5ca0f7
Building NodeJS cartridge Preparing build for deployment
Deployment id is c2527992
Activating deployment
Starting NodeJS cartridge Sat Apr 30 2016 10:41:09 GMT-0400 (EDT):
Starting application 'profile' ...
Script = server.js
Script Args = Node Options = !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! It is highly recommended that you add a package.json file to your application. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Waiting for application port (8080) become available ...
Application 'profile' failed to start (port 8080 not available) -------------------------
Git Post-Receive Result: failure
Activation status: failure
Activation failed for the following gears: 5724c3b42d5271363b000191
(Error activating gear: CLIENT_ERROR: Failed to execute: 'control start' for /var/lib/openshift/5724c3b42d5271363b000191/nodejs #<IO:0x000000019b0298> #<IO:0x000000019b0220> )
Deployment completed with status: failure postreceive failed
I read a couple of posts about some errors above like port 8080 not available and failed to execute control start but the directives I was able to follow did not solve my issue. I am finding the line that says "using a package.json file is highly recommended" strange as I do have one. My package.json file is:
{
"name": "Portfolio_Memoria",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node server.js"
},
"main": "server.js",
"description": "Portfolio_Memoria",
"author": {
"name": "gorra",
"email": ""
},
"dependencies": {
"express": "~4.9.0",
"body-parser": "~1.8.1",
"cookie-parser": "~1.3.3",
"morgan": "~1.3.0",
"serve-favicon": "~2.1.3",
"debug": "~2.0.0",
"jade": "~1.6.0",
"stylus": "0.42.3"
}
}
And server.js file is:
#!/usr/bin/env node
var debug = require('debug')('Portfolio_Memoria');
var app = require('./app');
if(typeof process.env.OPENSHIFT_NODEJS_PORT === 'undefined'){
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
} else {
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 3000);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');
var server = app.listen(app.get('port'), app.get('ip'), function() {
debug('Express server listening on port ' + server.address().port);
});
}
Of course, the application runs without issue locally. I don't know what I am missing here.
EDIT:
I got it to work by creating a blank application in OpenShift, cloning the repository OpenShift creates via command line, copying my whole project to it and pushing it back. This is a workaround and not a solution to the original problem, though.
I would like to use Node/ExpressJS to serve multiple apps, and I'm coming from an IIS background. The issue I'm having is based around restarting individual apps, without restarting all apps.
In IIS, I would simply click on the app (in the 'sites' list) and click on restart, which would restart only that app, leaving all others up and running.
This is what I have...
Server:
// index.js
require('console.table');
var _ = require('underscore-contrib');
var express = require('express');
var vhost = require('vhost')
var app = express();
var config = require("./config.json");
var websitesStart = function(website){
if (website.live) {
var host = vhost(website.domain, require(website.folder))
app.use(host);
}
}
_.each(config.websites, websitesStart);
app.listen(config.port, function(){
console.log('Web Server Running, Port:' + config.port);
console.table(config.websites);
});
Config:
// config.json
{
"port": "8080",
"websites": [
{
"name": "App 1",
"live": "true",
"domain": "app1.uk",
"folder": "../apps/app1"
},{
"name": "App 2",
"live": "true",
"domain": "app2.uk",
"folder": "../apps/app2"
},{
"name": "App 3",
"live": "true",
"domain": "app3.uk",
"folder": "../apps/app3"
}
]
}
Running node index.js starts all my apps just fine.
If I make a change to some code in App 2 for example, how would I restart only that app..? Leaving apps 1 and 3 running..?
UPDATE: I'm running on a Windows OS, so PM2 isn't really an option as it's only in beta.
UPDATE 2 I would really like to run all the apps on port 80, like I can in IIS. Wouldn't I need to use different ports if I ran each app as a different ExpressJS app (running them as separate processes/services)..?
You can use PM2 module for process management.
Just describe your apps in package.json and start them all like:
./node_modules/.bin/pm2 start package.json
After you can use this to restart one app:
./node_modules/.bin/pm2 restart <app_name|id|'all'|json_conf>
So you don't need "websitesStart" function at all.
Im using node with express and when I create server im listening to port 3000
in app.js by code,my question if there is any way to define it (host &port ) from exteranl file like some config json or something.if I want to be able to provide it from file .
You can "require" a config.
your config.json (can be js too).
{
"environment": "development",
"other": "value"
}
in app.js:
var cfg = require('./config.json');
Now you can access cfg properties.
Create config file, config.json:
{
"host": "localhost",
"port": 3000
}
Use require() to read it:
var express = require('express');
var app = express();
var config = require("./config.json");
app.listen(config.port, config.host, ...