Connect to database from server.js file on Angular and NodeJS app
I have two files in my angular project: server.js and db_connection.js. I'm hosting the project on Heroku.
The server.js currently runs because, in my package.json, I have "start": "node server.js"
I want to connect to the database as well, but I want to put that in a separate file called db_connection.js. How should I go about making sure the code in that file runs? Do I call it from server.js? If so, how so?
My files:
server.js:
const express = require('express');
const app = express();
const path = require('path');
app.use(express.static(__dirname + '/dist/my-app'));
app.listen(process.env.PORT || 8080);
// PathLocationStrategy
app.get('*', function (req, res) {
res.sendFile('dist/my-app/index.html' , { root : __dirname});
});
console.log('Console Listening')
db_connection.js:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "my_server.net",
user: "my_username",
password: "my_password"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
node.js angular heroku angular6
add a comment |
I have two files in my angular project: server.js and db_connection.js. I'm hosting the project on Heroku.
The server.js currently runs because, in my package.json, I have "start": "node server.js"
I want to connect to the database as well, but I want to put that in a separate file called db_connection.js. How should I go about making sure the code in that file runs? Do I call it from server.js? If so, how so?
My files:
server.js:
const express = require('express');
const app = express();
const path = require('path');
app.use(express.static(__dirname + '/dist/my-app'));
app.listen(process.env.PORT || 8080);
// PathLocationStrategy
app.get('*', function (req, res) {
res.sendFile('dist/my-app/index.html' , { root : __dirname});
});
console.log('Console Listening')
db_connection.js:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "my_server.net",
user: "my_username",
password: "my_password"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
node.js angular heroku angular6
Require is used for module resolution within nodejs, have a look at medium.freecodecamp.org/…
– Uzer
Nov 12 '18 at 23:00
Will you be executing your queries from db_connection.js file. Or will you use this file just for database connection only?
– Apar Adhikari
Nov 13 '18 at 6:12
add a comment |
I have two files in my angular project: server.js and db_connection.js. I'm hosting the project on Heroku.
The server.js currently runs because, in my package.json, I have "start": "node server.js"
I want to connect to the database as well, but I want to put that in a separate file called db_connection.js. How should I go about making sure the code in that file runs? Do I call it from server.js? If so, how so?
My files:
server.js:
const express = require('express');
const app = express();
const path = require('path');
app.use(express.static(__dirname + '/dist/my-app'));
app.listen(process.env.PORT || 8080);
// PathLocationStrategy
app.get('*', function (req, res) {
res.sendFile('dist/my-app/index.html' , { root : __dirname});
});
console.log('Console Listening')
db_connection.js:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "my_server.net",
user: "my_username",
password: "my_password"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
node.js angular heroku angular6
I have two files in my angular project: server.js and db_connection.js. I'm hosting the project on Heroku.
The server.js currently runs because, in my package.json, I have "start": "node server.js"
I want to connect to the database as well, but I want to put that in a separate file called db_connection.js. How should I go about making sure the code in that file runs? Do I call it from server.js? If so, how so?
My files:
server.js:
const express = require('express');
const app = express();
const path = require('path');
app.use(express.static(__dirname + '/dist/my-app'));
app.listen(process.env.PORT || 8080);
// PathLocationStrategy
app.get('*', function (req, res) {
res.sendFile('dist/my-app/index.html' , { root : __dirname});
});
console.log('Console Listening')
db_connection.js:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "my_server.net",
user: "my_username",
password: "my_password"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
node.js angular heroku angular6
node.js angular heroku angular6
edited Nov 12 '18 at 22:46
georgeawg
32.9k104967
32.9k104967
asked Nov 12 '18 at 22:43
James MitchellJames Mitchell
77031332
77031332
Require is used for module resolution within nodejs, have a look at medium.freecodecamp.org/…
– Uzer
Nov 12 '18 at 23:00
Will you be executing your queries from db_connection.js file. Or will you use this file just for database connection only?
– Apar Adhikari
Nov 13 '18 at 6:12
add a comment |
Require is used for module resolution within nodejs, have a look at medium.freecodecamp.org/…
– Uzer
Nov 12 '18 at 23:00
Will you be executing your queries from db_connection.js file. Or will you use this file just for database connection only?
– Apar Adhikari
Nov 13 '18 at 6:12
Require is used for module resolution within nodejs, have a look at medium.freecodecamp.org/…
– Uzer
Nov 12 '18 at 23:00
Require is used for module resolution within nodejs, have a look at medium.freecodecamp.org/…
– Uzer
Nov 12 '18 at 23:00
Will you be executing your queries from db_connection.js file. Or will you use this file just for database connection only?
– Apar Adhikari
Nov 13 '18 at 6:12
Will you be executing your queries from db_connection.js file. Or will you use this file just for database connection only?
– Apar Adhikari
Nov 13 '18 at 6:12
add a comment |
1 Answer
1
active
oldest
votes
You could do something like this.
server.js
const express = require('express');
const app = express();
const path = require('path');
const db = require('./db_connection')
app.use(express.static(__dirname + '/dist/my-app'));
app.listen(process.env.PORT || 8080);
// PathLocationStrategy
app.get('*', function (req, res) {
res.sendFile('dist/my-app/index.html' , { root : __dirname});
});
app.get('/getSomethingFromDB', db.getSomethingFromDB)
app.post('/postSomethingToDB', db.postSomethingToDB)
console.log('Console Listening')
db_connection.js
const mysql = require('mysql')
const con = mysql.createConnection({
host: "my_server.net",
user: "my_username",
password: "my_password",
database: 'my_db_name'
});
con.connect( err => {
if (err) throw err;
console.log("Connected")
});
module.exports = {
getSomethingFromDB: (req, res) => {
const getQuery = `SELECT * FROM some_table WHERE condition`
con.query(getQuery, (error, results, fields) => {
if (error) {
return res.status(500).send("DB Error")
} else {
return res.status(200).send(results)
}
})
},
postSomethingToDB: (req , res) => {
// Your code for post requests
},
// Similarly other functions
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271167%2fconnect-to-database-from-server-js-file-on-angular-and-nodejs-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could do something like this.
server.js
const express = require('express');
const app = express();
const path = require('path');
const db = require('./db_connection')
app.use(express.static(__dirname + '/dist/my-app'));
app.listen(process.env.PORT || 8080);
// PathLocationStrategy
app.get('*', function (req, res) {
res.sendFile('dist/my-app/index.html' , { root : __dirname});
});
app.get('/getSomethingFromDB', db.getSomethingFromDB)
app.post('/postSomethingToDB', db.postSomethingToDB)
console.log('Console Listening')
db_connection.js
const mysql = require('mysql')
const con = mysql.createConnection({
host: "my_server.net",
user: "my_username",
password: "my_password",
database: 'my_db_name'
});
con.connect( err => {
if (err) throw err;
console.log("Connected")
});
module.exports = {
getSomethingFromDB: (req, res) => {
const getQuery = `SELECT * FROM some_table WHERE condition`
con.query(getQuery, (error, results, fields) => {
if (error) {
return res.status(500).send("DB Error")
} else {
return res.status(200).send(results)
}
})
},
postSomethingToDB: (req , res) => {
// Your code for post requests
},
// Similarly other functions
}
add a comment |
You could do something like this.
server.js
const express = require('express');
const app = express();
const path = require('path');
const db = require('./db_connection')
app.use(express.static(__dirname + '/dist/my-app'));
app.listen(process.env.PORT || 8080);
// PathLocationStrategy
app.get('*', function (req, res) {
res.sendFile('dist/my-app/index.html' , { root : __dirname});
});
app.get('/getSomethingFromDB', db.getSomethingFromDB)
app.post('/postSomethingToDB', db.postSomethingToDB)
console.log('Console Listening')
db_connection.js
const mysql = require('mysql')
const con = mysql.createConnection({
host: "my_server.net",
user: "my_username",
password: "my_password",
database: 'my_db_name'
});
con.connect( err => {
if (err) throw err;
console.log("Connected")
});
module.exports = {
getSomethingFromDB: (req, res) => {
const getQuery = `SELECT * FROM some_table WHERE condition`
con.query(getQuery, (error, results, fields) => {
if (error) {
return res.status(500).send("DB Error")
} else {
return res.status(200).send(results)
}
})
},
postSomethingToDB: (req , res) => {
// Your code for post requests
},
// Similarly other functions
}
add a comment |
You could do something like this.
server.js
const express = require('express');
const app = express();
const path = require('path');
const db = require('./db_connection')
app.use(express.static(__dirname + '/dist/my-app'));
app.listen(process.env.PORT || 8080);
// PathLocationStrategy
app.get('*', function (req, res) {
res.sendFile('dist/my-app/index.html' , { root : __dirname});
});
app.get('/getSomethingFromDB', db.getSomethingFromDB)
app.post('/postSomethingToDB', db.postSomethingToDB)
console.log('Console Listening')
db_connection.js
const mysql = require('mysql')
const con = mysql.createConnection({
host: "my_server.net",
user: "my_username",
password: "my_password",
database: 'my_db_name'
});
con.connect( err => {
if (err) throw err;
console.log("Connected")
});
module.exports = {
getSomethingFromDB: (req, res) => {
const getQuery = `SELECT * FROM some_table WHERE condition`
con.query(getQuery, (error, results, fields) => {
if (error) {
return res.status(500).send("DB Error")
} else {
return res.status(200).send(results)
}
})
},
postSomethingToDB: (req , res) => {
// Your code for post requests
},
// Similarly other functions
}
You could do something like this.
server.js
const express = require('express');
const app = express();
const path = require('path');
const db = require('./db_connection')
app.use(express.static(__dirname + '/dist/my-app'));
app.listen(process.env.PORT || 8080);
// PathLocationStrategy
app.get('*', function (req, res) {
res.sendFile('dist/my-app/index.html' , { root : __dirname});
});
app.get('/getSomethingFromDB', db.getSomethingFromDB)
app.post('/postSomethingToDB', db.postSomethingToDB)
console.log('Console Listening')
db_connection.js
const mysql = require('mysql')
const con = mysql.createConnection({
host: "my_server.net",
user: "my_username",
password: "my_password",
database: 'my_db_name'
});
con.connect( err => {
if (err) throw err;
console.log("Connected")
});
module.exports = {
getSomethingFromDB: (req, res) => {
const getQuery = `SELECT * FROM some_table WHERE condition`
con.query(getQuery, (error, results, fields) => {
if (error) {
return res.status(500).send("DB Error")
} else {
return res.status(200).send(results)
}
})
},
postSomethingToDB: (req , res) => {
// Your code for post requests
},
// Similarly other functions
}
answered Nov 13 '18 at 8:27
Apar AdhikariApar Adhikari
159112
159112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271167%2fconnect-to-database-from-server-js-file-on-angular-and-nodejs-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Require is used for module resolution within nodejs, have a look at medium.freecodecamp.org/…
– Uzer
Nov 12 '18 at 23:00
Will you be executing your queries from db_connection.js file. Or will you use this file just for database connection only?
– Apar Adhikari
Nov 13 '18 at 6:12