WebSQL access in Electron application - node.js

I have an Electron application running a Node Express server. The Electron main process is:
var express = require('express');
var app = express();
app.get('/', function() {
/* I want to use WebSQL here */
res.send('Hi !!!');
});
app.listen(3636, function() {
console.log('Listen ...');
});
var window = new BrowserWindow();
window.loadURL('http://localhost:3636');
I want to use WebSQL inside my Node Express controllers but, i tried with openDatabase command and obviouslly i received "undefined function" error.
Any ideas ?

The reason you get undefined function is that you're trying openDatabase in node and not in the browser. If you still really want to use websql but in node you can try out https://github.com/nolanlawson/node-websql#readme (npm install websql -S to add it to your package.json)

Related

When running script for express web server with node on windows get prompt to select app

I'm going through this this course currently.
There is a tutorial on how to configure and run express server. The following script is suggested:
var express = require('express');
var path = require('path');
var open = require('open');
var port = 3000;
var app = express();
app.get('/',function(req, res){
res.sendFile(path.join(__dirname,'../src/index.html'));
});
app.listen(port,function(err){
if(err){
console.log(err);
}
else{
open('localhost:'+port);
}
});
When I run it in project root with $ node buildScripts/srcServer.js I get a system prompt You'll need a new app to open this localhost with the only suggestion being look at windows store.
What's that about? What does it need an app for? In the course when the script is run the browser is opened, but it's on Mac there. When I navigate manually to localhost:3000 there is the error like there is supposed to be, but I'm somewhat concerned that this behavior will mess with live reloading so I'd like to get rid of it.
Add the http:// prefix and it will open using the default browser.
open('http://localhost:'+port);

Node starts my server, so does gulp, but gulp doesn't find any paths

I am sure this is something small, but I am unable to figure it out. Node, nodemon and gulp use app.js to start my server so I am not sure why one way works while gulp does not. If I start my server using:
nodemon app.js or
node app.js it starts fine and when I use the path localhost:1337 in the browser it does what it is supposed to do.
When I use the command:
gulp
it says that it started the server but when I navigate to localhost:1337 it does not display anything other than "Cannot GET /" and none of my sources are showing in the inspector. I still get the message that "the port is listening" as indicated in my app.js console.log. My gulpfile is as follows:
var gulp = require('gulp');
var nodemon = require('gulp-nodemon');
gulp.task('nodemon', function(){
nodemon({
script: './app/app.js'
})
});
gulp.task('watch', function(){
gulp.watch('*.js', ['nodemon']);
});
gulp.task('default', ['nodemon']);
my app.js file is:
express = require('express');
var app = express();
app.use(express.static('../views'));
app.use(express.static('../bower_components/angular'));
app.use(express.static('./directives'));
app.listen(1337);
console.log("The port is listening");
The 'Cannot GET /' being displayed when you go to localhost:1337 is Express's generic message when a request is made against a route that is undefined. A route needs to be defined for the pages that you want to serve. You need to add a Route to handle the request to the root of your app at http://localhost:1337/.
app.get('/', function(req, res) {
// return back a message
res.send('Welcome!');
});
Place the above snippet above app.listen() and the message "Welcome!" will be displayed.
To serve a view in an html file you need to add a view engine so Express knows how to render the file. Additionally, the views property needs to be configured so Express knows where to render your views from. The example below uses ejs to just handle plain html files and not worry about a template engine like jade.
app.use('views', path.join(__dirname + '/views'));
app.engine('html', require('ejs').renderFile);
app.use('view engine', 'html');
Now we can use the render function on the res object to send our index.html file:
app.get('/', function(req, res) {
res.render('index');
});
This a guide from ExpressJS on how to implement routing in Express.

Express.js App on Phusion Passenger - did not write a startup response in time

I am trying to run an express.js app on a server running Phusion Paggenger (apache) and am seeing the error "An error occurred while starting the web application: it did not write a startup response in time." after the request times out. I've read through https://github.com/phusion/passenger/wiki/Debugging-application-startup-problems but this seems a bit obscure. My express app is as bare-bones as possible so I'm wondering if anyone knows if there may be a component specific to express that might cause this. I have been able to run a plain node.js app with the same setup on the server.
If you used the express-generator command to set up your project, you might see if pointing your Virtual Host configuration file's PassengerStartupFile line to bin/www instead of app.js does the trick instead of explicitly calling app.listen in the app.js file. Phusion Passenger's documentation does not address this specific convention adopted by ExpressJS. You can read some about this bin/www startup convention on Express's Moving to 4.x guide. Seemed to work for me.
It seems that you need to explicitly call app.listen within app.js. Specifically, I do this only when in production:
if (app.get('env') === 'production') {
app.listen(3000);
}
at the end of app.js
If you are getting here from google. This is now documented with Passenger: https://www.phusionpassenger.com/library/indepth/nodejs/reverse_port_binding.html
A working example of a simple express app is below:
if (typeof(PhusionPassenger) != 'undefined') {
PhusionPassenger.configure({ autoInstall: false });
}
var express = require('express');
var app = express();
app.get('/', function(req, res){
var body = 'Hello World';
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Length', body.length);
res.end(body);
});
if (typeof(PhusionPassenger) != 'undefined') {
app.listen('passenger');
} else {
app.listen(3000);
}

Unable to get connect-livereload to work with express server in gulp task

I am working off of Yeoman's gulp-webapp generator. I have modified my gulp serve task to use my Express server, rather than the default connect server it ships with. My issue is with Livereload functionality. I am trying to simply port the connect-livereload to work with my Express server rather than having to install new dependencies. It's to my understanding that most connect middleware should work fine with Express, so I am assuming connect livereload is compatible with Express 4.
Here are the contents of the relevant tasks in my gulpfile:
gulp.task('express', function() {
var serveStatic = require('serve-static');
var app = require('./server/app');
app.use(require('connect-livereload')({port: 35729}))
.use(serveStatic('.tmp'));
app.listen(3000);
});
gulp.task('watch', ['express'], function () {
$.livereload.listen();
// watch for changes
gulp.watch([
'app/*.ejs',
'.tmp/styles/**/*.css',
'app/scripts/**/*.js',
'app/images/**/*'
]).on('change', $.livereload.changed);
gulp.watch('app/styles/**/*.css', ['styles']);
gulp.watch('bower.json', ['wiredep']);
});
gulp.task('styles', function () {
return gulp.src('app/styles/main.css')
.pipe($.autoprefixer({browsers: ['last 1 version']}))
.pipe(gulp.dest('.tmp/styles'));
});
gulp.task('serve', ['express', 'watch'], function () {
require('opn')('http://localhost:3000');
});
With this simple setup, when I run gulp serve in my cmd everything spins up fine and I can accept requests at http://localhost:3000.
Now if I go and change the body's background color from #fafafa to #f00 in main.css and hit save, my gulp output will respond with main.css was reloaded, as seen in the bottom of this screenshot.
However, my webpage does not update. The background color is still light-grey instead of red.
Is there perhaps a conflict between my express server config and the way gulp handles its files? Is my Express server forcing the use of app/styles/main.css rather than the use of .tmp/styles/main.css? Shouldn't the livereload script handle the injection of the new temporary file?
Thanks for any help.
EDIT:
I was able to move forward a bit by adding livereload.js to the script block of my index file, like so:
<script src="http://localhost:35729/livereload.js"></script>
I am now able to get live changes pushed to the client. Why was this file not getting injected before? How can I ensure this is getting used programatically as opposed to pasting it into my files?
I was able to get past this issue by removing the app.use(require('connect-livereload')({port: 35729})) from my gulpfile, along with a couple of other lines, and having that instantiate in my Express server's app.js file.
My gulpfile's express task now looks like this:
gulp.task('express', function() {
var app = require('./server/app');
app.listen(3000);
});
I added in the connect-livereload just above where I specify my static directory in Express:
if (app.get('env') === 'development') {
app.use(require('connect-livereload')());
}
app.use(express.static(path.join(__dirname, '../app')));
Once I started using this setup, I was getting the livereload.js script injected into my document, and client-side changes are now auto-refreshed just how I wanted.
Hope this helps someone!

socket.io keeps 404ing?

sorry for the silly question and im sure its a noob mistake. I am following the how to use for socket io: http://socket.io/#how-to-use and keep having an problem;
I have a node app (running version 3.0 alpha of express) and have the following:
app = express()
io = require('socket.io').listen app
I have edited the layout.jade and added:
script(src="/socket.io/socket.io.js")
I have also run npm install socket.io and confirmed that it starts fine on the server.
If I browse to any page, console keeps showing:
GET http://localhost:3000/socket.io/socket.io.js 404 (Not Found)
Anyone else experienced this issue?
Here are step by step instructions I just tested them
express socket
cd socket
npm install
npm install socket.io
Add the following in app.js
var io = require('socket.io').listen(app);
Run
node app
In the console you should see
info - socket.io started
In the browser go to
http://localhost:3000/socket.io/socket.io.js
You should see the raw JavaScript
Edit:
I'm also having problems with 3.0alpha1. Looks like a bug. Here is an ugly work around
var fs = require('fs');
app.get('/socket.io/socket.io.js', function(req, res) {
fs.readFile('/PROJECT_HOME/node_modules/socket.io/lib/socket.io.js', function(error, content) {
if (error) {
res.writeHead(500);
res.end();
}
else {
res.writeHead(200, { 'Content-Type': 'text/javascript' });
res.end(content, 'utf-8');
}
});
});
Are you runnning layout.jade from the app.js?
app.get('/',function(req,res){
res.sendfile('index.html');
});
This needs to be included in the app.js assuming your template is in index.html file.

Resources