Need to RUN SQL Stored Procedure from JavaScript - javascript

I'm testing the API from the Cypress tool. For my requirement I have connected to my SQL Server DB using coffeescript and tedious npm packages with the required settings. With that I'm also able to run the SQL queries like this...
cy.sqlServerDB2("SQL Query...")
But now I have got a 200 line SQL Script which have placed in the root directory as "CreatePhysicalData.sql".
Now my requirement is to run the "CreatePhysicalData.sql" script.
Can someone help me with it please.
Thanks

Related

How to restart a Node.js project?

I'm not a Node.js developer. So I have no idea how it works. I've been a PHP developer for over 8 years.
Because of some reason, I need to make a small change in a Node.js project which is live. All I have to do is changing a payment gateway token. I did it like this:
After pulling it on the server, users still go to the old payment gateway. So I guess I need to do a restart. (I'm saying so because, for PHP projects, when you change a config-related thing, you need to restart PHP).
Not sure should I restart what thing? Noted that, the server is Ubuntu 20.04 and uses Nginx to talk to Node.js. In other word, how can I see Node is running as what service on Linux?
Also, there are two files that I think I need to run the project again after restarting Node through one of them: index.js, server.js. Am I right?
And
Your Node.js script likely runs under a process that restarts the script in case it dies. There are several "run forever" wrappers, the most popular one is pm2. Find out which one is used in your project. Try pm2 list as the user your project executes under. If pm2 type pm2 restart app_name to restart your project.
Please check if it is a node.js project so you can write the command node index.js or node server.js with this command you can start your node server.

PHP unable to identify sqlsrv_connect() function while trying to connect SQL Server

I'm trying to connect to my local SQL Server from a simple PHP file using the sqlsrv_connect() function, but every time I'm calling the file in the browser through localhost, it's throwing a 500 (Internal Server Error) saying:
PHP Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in C:\inetpub\wwwroot\AJAX_Tutorial\get_db_data.php:4
get_db_data.php is the file from which I'm trying to connect the server. Seems like PHP or the localhost can't identify the sqlsrv_connect() function. But as far I'm concerned, I did all the needful to make sure PHP connects the SQL Server.
My environment: Windows 10 Pro, Version 21H2, 64-bit.
What I have done:
Enabled IIS ensuring CGI/Fast CGI is working.
Installed PHP 8.1.1 non-thread safe x64 version at C:\Program Files\PHP-8.1.1
In C:\Program Files\PHP-8.1.1, renamed php.ini-development file to php.ini
In the php.ini file, uncommented the extension_dir = "ext" directive.
Downloaded php_wincache.dll and added it to the default ext directory of PHP.
Added the line extension=php_wincache.dll at the Dynamic Extensions section of the php.ini file.
Installed PHPManagerForIIS_V1.5.0 and configured IIS accordingly so that PHP can be hosted through IIS. Also enabled the php_wincache.dll extension here.
Installed SQL Server 2019 Developer Edition along with the respective Management Studio.
Created the respective database and tables in SQL Server that I want to connect to from PHP.
Ensured Microsoft ODBC Driver 17 for SQL Server is installed in my PC, that is required by PHP.
Ensured Microsoft SQL Server 2012 Native Client is installed in my PC, that is required by PHP.
Downloaded Microsoft Drivers for PHP for SQL Server 5.9 and extracted its contents. Copied the file named php_sqlsrv_80_nts_x64.dll in the package and pasted it in the default ext directory of PHP.
Added the line extension=php_sqlsrv_80_nts_x64.dll at the Dynamic Extensions section of the php.ini file.
In IIS Manager, through PHP manager, enabled the php_sqlsrv_80_nts_x64.dll extension. Created a phpinfo.php file in the root of the IIS, which ran successfully but found no mention of wincache and sqlsrvin it.
After the steps above, I ran the actual PHP file trying to connect the SQL Server, but it's throwing an error saying it can't identify the sqlsrv_connect() function. Assuming the php_sqlsrv_80_nts_x64.dll not being loaded while PHP is starting, I ran php --ini in the command prompt.
That's when the following messages are being thrown:
PHP Warning: PHP Startup: Unable to load dynamic library 'php_wincache.dll' (tried: ext\php_wincache.dll (The specified module could not be found), ext\php_php_wincache.dll.dll (The specified module could not be found)) in Unknown on line 0
Warning: PHP Startup: sqlsrv: Unable to initialize module
Module compiled with module API=20200930
PHP compiled with module API=20210902
These options need to match
in Unknown on line 0
Configuration File (php.ini) Path:
Loaded Configuration File: C:\Program Files\PHP-8.1.1\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
However, PHP seems to be running fine, because when I used jQuery AJAX get() and post() method from an HTML file to fetch data from another PHP file, I was successful in doing so. No exception was thrown then.
So what am I missing now that neither php_wincache.dll and sqlsrv seem to load during PHP startup, nor can I connect the SQL Server from the PHP file? As I'm new jQuery AJAX and PHP, I'm not much aware of the intricacies of them and hence, stuck with the issue for the past four days. I've used every resource in my hand, but nothing is working. Please help. I can't get ahead with my tasks because of this.
get_db_data.php code:
<?php
$serverName = "(local)"; // Optionally use port number (1433 by default).
$connectionString = array("Database"=>"TestDB"); // Connection string.
$conn = sqlsrv_connect($serverName, $connectionString); // Connect using Windows Authentication.
if($conn === false) {
echo "Connection could not be established.<br/>";
die(print_r(sqlsrv_errors(), true));
} else {
echo "Connection established successfuly.<br/>";
}
sqlsrv_close($conn); // Close connection resources.
?>
Thanks and Regards!
I found the root of the problem, i.e., version mismatch. While I was using PHP-8.1, I downloaded the SQLSRV for PHP-8.0. Because of this match the driver file wasn't loading during the start of the PHP in the server. Downgrading to PHP-8.0 solved the issue.

Live console input/output for a gameserver using JavaScript or PHP

General information: I have a Minecraft server running in the basement for some friends that is running for a couple months now. We were using TeamViewer to input commands into the console and view possible errors while the server was running. The console of a Minecraft server basically shows you a log of events with timestamps as well as a command input line at the bottom that enables you to execute in-game commands with administrator rights.
The console is started via a batch file:
:startup
#echo off
set v1=6
cls
Java -Xmx%v1%G -jar spigot.jar nogui
goto startup
The spigot.jar file is the file that basically runs the server.
Because we manage multiple servers and the TeamViewer process is quite insecure and slow, i wanted to find other ways to access the console.
Target: I want to create a HTML webinterface that uses JavaScript or PHP to communicate with the server console. It should be able to show the live console log as well as being able to type commands in the browser that are then executed in the commandline on the server.
Problems:
Is there any way to send a JavaScript/PHP input to the console so that I can execute commands from a webinterface?
Can I save the live console output to a .txt file that the JavaScript/PHP script can then display live on the webinterface?
I would definitely recommend moving your setup outside of a Windows environment, as you're going to have a much easier time hosting a PHP server, etc. in a Linux environment.
Admittedly, I'm not familiar with MineCraft, but it looks like the batch file you have is effectively just launching a Java JAR which can be done from any computer with the JRE installed. Java console apps read input on the STDIN of a process and output on STDOUT for any application-generated output. This is incredibly easy to pickup in a Ubuntu environment.
For example, let's say you install the standard LAMP stack on a fresh Ubuntu install. You'll have PHP for running your server-side process, and Apache for hosting any web interface you decide to build. Keeping a focus on frontend vs. backend, I'd probably try to setup an API in PHP using Slim or another lightweight framework. In the API endpoint you use to start your server, it would look something like this:
exex('screen -d -m -S ServerOne');
exec('screen -S ServerOne -p 0 -X stuff "java -Xmx%v1%G -jar spigot.jar nogui > /some_path_to_log^M"');
The first exec() will create a screen session named "ServerOne" inside of which your server process can run. The second exec() will send the command needed to start your server process inside of the screen session. Effectively, this will run your server asynchronously to your PHP script and write the output to the specified path. Knowing where to expect the output, in the API endpoint you use to get your log, you'd have something like:
$logContent = file_get_content('/some_path_to_log');
Finally, in the API endpoint you use to write commands to your server, you would have something that looks like:
exec('screen -S ServerOne -p 0 -X stuff "<your_command_here>^M"');
This will write to the STDIN stream of your server's Java process which should, in turn, be interpreted as if you were actually typing at the keyboard of the console itself.
That's the basics of it, or at least where I'd get started. There are some pretty cool things that you could do with WebSockets to open a live I/O session for your server's console, but it'd be a bit more involved than this post. If you're interested, check out Ratchet for PHP or ws for Node.js.
Consider this ServerFault post for more information on sending input to screen sessions.
I suppose you could show the log on a website like this:
first gameserver script:
:startup
#echo off
set v1=6
cls
Java -Xmx%v1%G -jar spigot.jar nogui > log.txt
fileup log.txt
goto startup
second gameserver script(called fileup.bat){replace myusername, mypassword and servername.com with the credentials if your php server}:
#echo off
echo user MyUserName> ftpcmd.dat
echo MyPassword>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat SERVERNAME.COM
del ftpcmd.dat
php-server side script:
<?php
$filename = "log.txt";
$fp = fopen($filename, "r");
$content = fread($fp, filesize($filename));
$lines = explode("\n", $content);
fclose($fp);
print_r($lines);
?>
by the way: I have not tested any of these scripts and they might not work, I just based this on my knowledge of php and batch. Also, the second batch script needs to be in the same directory as the first batch script and you do not have to run the second batch script
Note:
I am an experienced PHP user but I do not know very much about batch(I use linux) but I suppose this will work and I have used batch very often in the past. Also I recommend using the PHP script on an external hosted php server from 000webhost.com or infinityfree.net because they are free and save you a lot of time + they have all the needed ftp things preconfigured.
Please let me know if this worked

DynamoDB Local deletes data after shutting down instance

I'm using DynamoDB Local for my app, and it keeps deleting all the sample data every time I shut down the instance. Does anyone know why this happens?
I've tried to look it up, but I don't see anyone else has this issue.
I've used the downloadable version of dynamodb and use this command dynamodb_local_latest baopham$ java -Djava.library.path=./DynoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb -inMemory to start and instance.
Am I missing anything? Thank you.
DynamoDB local usage documentation clearly states that if use -inMemory option, data will be in memory and data will be lost when you terminate. Take out -inMemory option in your command.
If you use the -inMemory option, DynamoDB does not write any database files at all. Instead, all data is written to memory, and the data is not saved when you terminate DynamoDB.
for docker, this command worked for me
docker run --name dynamodb -p 8000:8000 -d amazon/dynamodb-local -jar DynamoDBLocal.jar

How to use a browser on a remote server for an automated tests

I inherited a project where the person wrote tools to test our site's UI using JQuery and JS.
I don't know too much about it other than it requires a browser to be spawned and I think the tool uses JS to interact with iframes to see if it's the expected values.
My job is to get this tool to run on a remote server and post the results to Jenkins.
The remote test server and staging server is linux. From our staging server, I want to write a script to spawn a browser and run cmds from the tool to test our UI. I ran the following manually:
ssh -X user#remote_test_server /usr/bin/firefox
However, the remote server says:
Error: no display specified
Is there a way to spawn a browser for automated testing from one headless server to another? Thanks in advance for your help.
I faced a similar problem when I tried to automate a GUI installation program. While there are quite some different possibilities to choose from (e.g. Xnest, Xephyr?), I ended up using vncserver, because it's relatively easy to debug the GUI session this way.
You need to create a vncpassword file, I think:
mkdir -p $HOME/.vnc
chmod 0700 $HOME/.vnc
echo MyLittlePassword | vncpasswd -f > $HOME/.vnc/passwd
chmod 0600 $HOME/.vnc/passwd
Starting the server is then quite straightforward
vncserver
export DISPLAY=:1
/usr/bin/firefox&
...
Now it is possible to connect to the VNC server with a VNC viewer of your choice. But beware there may be no window manager, depending on the X startup scripts of your environment.
Shutting the server down
vncserver -kill :1
In the configuration of Jenkins project , specify the
Build Environment
Start Xvfb before the build, and shut it down after.
#

Categories