Integrate NodeJS MySql with React Application - javascript

I have a normal React App which runs on Node Server
-node_modules
-src
-Actions
-Components
-Stores
-Server
-server.js
-package.json
Basically when i run npm start React App will run and for suppose i will be able to see the example in localhost:8080
Now my server.js file includes MySql code
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
I have to explicitly run node server.js command to get the connection to mysql and run the queries their.
How do i integrate server.js command in my React App, so that when i run npm start, my MySql connection and the file should execute

The short answer is: you don't.
The short fix is: use some kind of program to help you start both (like npm)
The long answer is:
Set up your npm start task to run both of them. You can do this with concurrently. (npm install --save concurrently)
Change your npm start task (in your package.json) to something like this:
"start": "concurrently -k -r -s first \"node server.js\" \"webpack-dev-server""
If you need further assistance please share your package.json file.
The longest answer is:
This part is OPTIONAL, only follow it if you want to LEARN.
If you want more details you could look at a somewhat more advanced project such as my react-sane-starter to get an idea how to efficiently start multiple services. This project also contains Docker if you're interested.
concurrently over &
People often suggest to run one of the tasks in the background using &, this will usually prevent logs being shown (unless redirected), I'd highly recommend using concurrently to solve this issue. (it will prefix your different services quite nicely)

Related

Run command in `before_migrate.rb` as user with sudo privileges with AWS OpsWorks

In my Rails app, I'm trying to run a gulp task during deployment, which adds a css file to a directory inside the public directory. In the deployment log, I see that the task runs successfully, but when I SSH into the server, I don't see the file.
[2016-09-24T01:35:02+00:00] INFO: Processing execute[create critical css] action run (/srv/www/toaster/releases/20160924013146/deploy/before_migrate.rb line 43)
[2016-09-24T01:35:03+00:00] INFO: execute[create critical css] ran successfully
While I'm connected to the server via SSH and change my user to the deploy user with sudo su deploy and then run the gulp task, the file gets create.
What am I doing wrong?
The line in before_migrate looks like this...
execute 'create critical css' do
user 'deploy'
cwd release_path
command 'gulp criticalCss'
end
The task in my gulpfile looks like this...
gulp.task('criticalCss', function() {
glob('./public/assets/theme_templates/v3/application-*.css', function(err, matches) {
var full_css = matches[0]
penthouse({
url : 'http://performance-site.s2.fanbread.com/blogs/performance-benchmark',
css : full_css,
width: 375,
height: 667
}, function(err, criticalCss) {
console.log(err)
fs.writeFile('./public/assets/theme_templates/v3/posts_show.css', criticalCss); // Write the contents to a jekyll include
});
});
});
Thanks
The usual solution for Chef would be to turn the log level up to DEBUG to see the command output. No idea how you do that with current Opsworks. Most of the time these issues are either related to an environment variable like $HOME not being set or that gulp isn't being found correctly.
As a side note, I do not recommend using the deploy resource anymore. A normal git resource is probably better in general. See my application_examples cookbook for an example of a more modern Rails deployment with Chef.

How to make sure a MySQL database exists before running Node.js app

I have a Node.js/Express based app that is using Sequelize to talk to a MySQL server. What is the best way to ensure that a specific database exists before starting the app using npm start? I guess it would be some kind of one-time database initialization script that runs CREATE DATABASE IF NOT EXISTS foo; - I am just not sure where to put it and how to hook it up to a lifecycle event.
Taken from the npm docs npm supports the "scripts" property of the package.json script, one of which is prestart, so you could have a package.json
{ "scripts" :
{
"prestart" : "node scripts/mysqlCheck.js",
"start" : "node index.js"
}
}
then in that mysqlCheck.js have your MySQL DB checks. Then when you run npm start the prestart script will be run before index.js.

How to keep my node js server alive

I have created an app in Node js including with express js and I will execute the app using the command prompt
c:/myApp > npm start
And it is running my app, but whenever I closed the prompt the server is closing.I have tried with forever like
> npm install forever -g
> forever start --minUptime 1000 --spinSleepTime 1000 app.js
but it is not working either.Is there any way to keep the connection alive.Thanks in advance.
I've used forever for about 3 years. It sucks. It has a ton of bugs and BS that hasn't been fixed after a long time.
I've switched to pm2. It has a much better CLI and API, and a lot less bugs.
CLI:
pm2 start app.js
API:
var pm2 = require('pm2')
pm2.connect(function(err) {
pm2.start({
script: 'runningTest.js'
}, function(err, apps) {
if(err) console.log(err)
pm2.disconnect()
})
})
I use this instruction (Nginx + supervisord) for my projects in production:
http://cuppster.com/2011/05/12/diy-node-js-server-on-amazon-ec2/
Start read from chapter "Have Your Node Server Run Forever"

Trouble using grunt connect server on heroku

All the tutorials I can find use some kind of app.js to run their node server on heroku.
I have already configured my grunt - connect task to use modRewrite for my single page app so that all requests simply return index.html. So it would be convenient for me to somehow just have heroku run my grunt dist once it npm installs.
I have tried adding this line to my package.json
"scripts": {
"postinstall": "echo postinstall time; ./node_modules/grunt/lib/grunt.js dist"
}
But I think there is something wrong with my path, because it doesn't work. I get a Permission denied error.
I also tried using a simple app.js express server (even though I would have to re-figure out how to do the modRewrite) -- but that failed somewhere along the line - heroku was looking for an index.js, which I don't have.
Thanks!
The solution I ended up with is a simple connect server using the same modRewrite code from my grunt task:
var connect = require('connect');
var modRewrite = require('connect-modrewrite');
var app = connect()
.use(modRewrite(
['^[^\\.]*$ /index.html [L]']
))
.use(connect.static('./dist/'))
.listen(process.env.PORT || 3000)
And the following Procfile:
web: node app.js

Starting a node.js server

I recently got into node and I installed it on my localhost. I am using WAMP. I am on Windows Vista.
Anwyay, I installed it. I made a new file in my localhost directory with this being called server.js
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*'
});
response.end('Hello World\n');
}).listen(1337);
then I went to node and tried typing % node server.js and all I got was an ellipses. What gives?
UPDATE: I checked my Systems variable and saw that my PATH lists the node.js as C:\Program Files (x86)\nodejs\
Run cmd and then run node server.js. In your example, you are trying to use the REPL to run your command, which is not going to work. The ellipsis is node.js expecting more tokens before closing the current scope (you can type code in and run it on the fly here)
The NodeJS code that you wrote is corrected, and it should work but as #dwerner said you have to write node server.js in the command prompt not inside the Node REPL.
But today most of who work with NodeJS are developing using a development environment (IDE). By using IDE you get a hotkey for running your code, and many things that can help you in the daily development (Syntax highlight for e.g.)
One of the most popular IDE's today for NodeJS development is VSCode, you can check it out.
As dwerner and aminadav mentioned, you need to run the node command for the main .js file you're using for your script/app. This file will typically be index.js by default, when you run npm init to create the package.json for your NodeJS project.
Maybe you will find this blog post that covers the basics helpful as well. :)
https://dev.to/bishopwm/my-first-server-and-rest-api-essentials-for-frontenders-2gnk

Categories