RequireJs doesn't work with mysql variable - javascript

I have a little Problem with RequireJs. When i load the index.js of mysql as a mysql variable and create a connection with it, then it says: Uncaught ReferenceError: createConnection is not defined. This is the code i use it in:
var mysql = requirejs(['mysql/index.js']);
var connection = mysql.createConnection({
host: 'myhost',
user: 'myuser',
password: 'mypassword',
database: 'mydatabase'
})
The javascript code is in the index.js of the directory js. And i use it in index.html.
The hierarchy
What did i wrong? I don't use the normal require because i can't call it with node and it doesn't work without it.
Thank you for any help.

https://www.npmjs.com/package/mysql is a NPM package so it uses CommonJS module format. It is possible to use CommonJS packages with RequireJS, please read more in the docs: https://requirejs.org/docs/commonjs.html

Related

I faced a problem when I used .env file in node.js

In my node.js project, I want to use environment variables for saving MongoDB's user name and password. When I directly use username and password then it is working completely. But when I used template string and process.env.varName then I got bad auth: Authentication failed error message.
ok: 0,<br/>
code: 8000,<br/>
codeName: 'AtlasError',<br/>
[Symbol(errorLabels)]: Set(0)<br/>
{}

adonisjs migration issue with mysql

I'm trying to get the hang of adonisjs through this tutorial
I set up everything properly as instructed in there but I run into a problem when I type adonis migration:run.
The error I get on Terminal is Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'#'localhost' (using password: NO)
I have 0 experience with mysql and this confuses me. I'd appreciate some guidance/help here.
Cheers!
#katotopark, this means your MySQL server is rejecting the connection from the Adonisjs application. check for a file called (.env) in the root folder of your project and inside you will need to add the credentials for the connection the MySQL server. (probably user: root, password: the password you use when you installed MySQL)
your .env should look similar to this.
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root/or-another-mysql-user
DB_PASSWORD=your-database-user-password***
DB_DATABASE=the_name_of_your_database
Regards.

How to add database environment variables to Javascript

I want to add my database environment variables to a .env file and use that file in my Javascript program in order to create a connection to my database using Node.js.
So here is my database info which I use to create connection with:
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "my password",
database: "mydatabase"
});
Then I try to check if I am connected to my database using these lines:
(It should print "Connected!").
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
I want to put the first block of code in another file and require that file in my Node.js program.
How should I do this? How should I require the file? Thank you in advance :)
I suggest using dotenv package.
Taken straight from the readme:
As early as possible in your application, require and configure dotenv.
require('dotenv').config()
Create a .env file in the root directory of your project. Add
environment-specific variables on new lines in the form of NAME=VALUE. For example:
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
Usage (process.env now has the keys and values you defined in your .env file.)
var db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})
NOTE: Make sure your .gitignore has an entry to ignore .env files.
You can use a module called dotenv which will load an environments variables defined in a .env file or any file you specify. You would then load the .env in two ways:
In the main script file, call require('dotenv').config()
Via npm script: "dev": "node -r dotenv/config app.js"
Either works, but you do not want to commit .env to source control. Always keep sensitive credentials out. You can create a an .env.example to alert new users the required variables.
Using dotenv package. Install dotenv package.
npm i dotenv
Create a new .env file in project root directory.
touch .env
Add environment variables to .env file
API_HOST=HOST-PLACEHOLDER-URL
API_KEY=TOP-SECRET
APP_NAME=node_crud
APP_ENV=local
APP_KEY=base64:RIjL2Uw/Wdve+HJEvRNp6LHhzoHtLbplQcUp60CBIvs=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=node_restapi
DB_USERNAME=root
DB_PASSWORD=
PORT = 5000
Add config/database.js file
const dotenv = require('dotenv');
const mysql = require('mysql');
// configraration with env.
dotenv.config();
module.exports = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE
});
config/database.js connect with app.js file
// Database Connection
const conn = require('./config/database');
// Shows Mysql Connect
conn.connect((err) =>{
if(err) throw err;
console.log('Mysql Connected with App...');
});
A .env file is needed for a clean separation of environment-specific configurations.
The dotenv packaged is used to read a .env file at runtime containing environment variables and append them on the process.env object.
Creating an example for a .env file to document the mandatory variables speeds up project setup time.
Never commit a .env file to version control.

I need to create a simple database for use in an Angular/Node demo. What is the path of least resistance to adding such a database to my demo?

I need to create a simple database for use in an Angular/Node demo. What is the path of least resistance to adding such a database to my demo?
Incidentally, I cannot use the MEAN stack.
Edit: I failed to mention that I'm running the demo from my local environment--from my laptop.
The path of least resistance would be to use Sequelize and sqlite3 in your application.
Start off by running npm install sequelize sqlite3.
Then in your code
var Sequelize = require( 'sequelize' )
var connection = Sequelize.connect( undefined, {
dialect: 'sqlite',
storage: __dirname + '/db.sqlite'
})
Tada, you have a database connection syncing to a local sqlite DB.
You'll likely need to consult the Sequelize documentation to use this connection.
If, however, you wanted an API for your angular demo, json-server is what you're looking for.

nodejs .env variable undefined

I am getting undefined when trying to get the value from .env
Here is my server.js
console.log(process.env.val);
Here is my .env file
val=hello
Screenshot that shows the file hierarchy
When I run the server I get undefined. How to fix?
You need to use dotenv library
Create a .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE. For example:
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
That's it.
process.env now has the keys and values you defined in your .env file.
var db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})

Categories

Resources