Browse Source

nodejs and express modifications for Windows

Pēteris Ņikiforovs 12 years ago
parent
commit
d17bdbe779
4 changed files with 71 additions and 14 deletions
  1. 11 4
      express/app.js
  2. 24 2
      express/setup.py
  3. 17 7
      nodejs/hello.js
  4. 19 1
      nodejs/setup.py

+ 11 - 4
express/app.js

@@ -5,11 +5,11 @@
 
 
 var cluster = require('cluster')
 var cluster = require('cluster')
   , numCPUs = require('os').cpus().length
   , numCPUs = require('os').cpus().length
+  , windows = require('os').platform() == 'win32'
   , express = require('express')
   , express = require('express')
   , mongoose = require('mongoose')
   , mongoose = require('mongoose')
   , async = require('async')
   , async = require('async')
   , conn = mongoose.connect('mongodb://localhost/hello_world')
   , conn = mongoose.connect('mongodb://localhost/hello_world')
-  , Mapper = require('mapper')
   , connMap = { user: 'benchmarkdbuser', password: 'benchmarkdbpass', database: 'hello_world', host: 'localhost' };
   , connMap = { user: 'benchmarkdbuser', password: 'benchmarkdbpass', database: 'hello_world', host: 'localhost' };
 
 
 var Schema = mongoose.Schema
 var Schema = mongoose.Schema
@@ -21,9 +21,12 @@ var WorldSchema = new Schema({
 }, { collection : 'world' });
 }, { collection : 'world' });
 var MWorld = conn.model('World', WorldSchema);
 var MWorld = conn.model('World', WorldSchema);
 
 
-Mapper.connect(connMap, {verbose: false, strict: false});
-var World = Mapper.map("World", "id", "randomNumber");
-var Fortune = Mapper.map("Fortune", "id", "message");
+if (!windows) {
+  var Mapper = require('mapper');
+  Mapper.connect(connMap, {verbose: false, strict: false});
+  var World = Mapper.map("World", "id", "randomNumber");
+  var Fortune = Mapper.map("Fortune", "id", "message");
+}
 
 
 if (cluster.isMaster) {
 if (cluster.isMaster) {
   // Fork workers.
   // Fork workers.
@@ -81,6 +84,8 @@ if (cluster.isMaster) {
   });
   });
 
 
   app.get('/mysql-orm', function(req, res) {
   app.get('/mysql-orm', function(req, res) {
+    if (!windows) return res.send(501, 'Not supported on windows');
+    
     var queries = req.query.queries || 1
     var queries = req.query.queries || 1
       , worlds  = []
       , worlds  = []
       , queryFunctions = [];
       , queryFunctions = [];
@@ -100,6 +105,8 @@ if (cluster.isMaster) {
   });
   });
 
 
   app.get('/fortune', function(req, res) {
   app.get('/fortune', function(req, res) {
+    if (!windows) return res.send(501, 'Not supported on windows');
+    
     Fortune.all(function (err, fortunes) {
     Fortune.all(function (err, fortunes) {
       var newFortune = {id: 0, message: "Additional fortune added at request time."};
       var newFortune = {id: 0, message: "Additional fortune added at request time."};
       fortunes.push(newFortune);
       fortunes.push(newFortune);

+ 24 - 2
express/setup.py

@@ -9,12 +9,34 @@ def start(args):
   setup_util.replace_text("express/app.js", "localhost", args.database_host)
   setup_util.replace_text("express/app.js", "localhost", args.database_host)
 
 
   try:
   try:
-    subprocess.check_call("npm install", shell=True, cwd="express")
-    subprocess.Popen("NODE_ENV=production node app", shell=True, cwd="express")
+    npm()
+    if os.name == 'nt':
+      subprocess.Popen("set NODE_ENV=production", shell=True)
+      subprocess.Popen("node app", shell=True, cwd="express")
+    else:
+      subprocess.Popen("NODE_ENV=production node app", shell=True, cwd="express")
     return 0
     return 0
   except subprocess.CalledProcessError:
   except subprocess.CalledProcessError:
     return 1
     return 1
+
+def npm():
+  if os.name == 'nt':
+    subprocess.check_call("copy package.json package.json.dist /y > NUL", shell=True, cwd="express")
+    setup_util.replace_text("express/package.json", ".*mysql.*", "")
+    setup_util.replace_text("express/package.json", ".*mapper.*", "")
+  
+  try:
+    subprocess.check_call("npm install", shell=True, cwd="express")
+  finally:
+    if os.name == 'nt':
+      subprocess.check_call("del package.json", shell=True, cwd="express")
+      subprocess.check_call("ren package.json.dist package.json", shell=True, cwd="express")
+
 def stop():
 def stop():
+  if os.name == 'nt':
+    subprocess.Popen("taskkill /f /im node.exe > NUL", shell=True)
+    return 0
+  
   p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
   p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
   out, err = p.communicate()
   out, err = p.communicate()
   for line in out.splitlines():
   for line in out.splitlines():

+ 17 - 7
nodejs/hello.js

@@ -1,5 +1,6 @@
 var cluster = require('cluster')
 var cluster = require('cluster')
-  , numCPUs = require('os').cpus().length;
+  , numCPUs = require('os').cpus().length
+  , windows = require('os').platform() == 'win32';
 
 
 if(cluster.isMaster) {
 if(cluster.isMaster) {
   // Fork workers.
   // Fork workers.
@@ -17,18 +18,22 @@ if(cluster.isMaster) {
 var http = require('http')
 var http = require('http')
   , url = require('url')
   , url = require('url')
   , async = require('async')
   , async = require('async')
-  , libmysql = require('mysql-libmysqlclient').createConnectionSync()
   , mongoose = require('mongoose')
   , mongoose = require('mongoose')
   , conn = mongoose.connect('mongodb://172.16.98.98/hello_world')
   , conn = mongoose.connect('mongodb://172.16.98.98/hello_world')
   , MongoClient = require('mongodb').MongoClient
   , MongoClient = require('mongodb').MongoClient
-  , Mapper = require('mapper')
   , connMap = { user: 'benchmarkdbuser', password: 'benchmarkdbpass', database: 'hello_world', host: 'localhost' };
   , connMap = { user: 'benchmarkdbuser', password: 'benchmarkdbpass', database: 'hello_world', host: 'localhost' };
 
 
+if (!windows) {
+  var Mapper = require('mapper')  
+    , libmysql = require('mysql-libmysqlclient').createConnectionSync();
+    
+    Mapper.connect(connMap, {verbose: false, strict: false});
+    var World = Mapper.map("World", "id", "randomNumber")
+    libmysql.connectSync('localhost', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
+}
+
 var collection = null;
 var collection = null;
-Mapper.connect(connMap, {verbose: false, strict: false});
-var World = Mapper.map("World", "id", "randomNumber")
 
 
-libmysql.connectSync('localhost', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
 MongoClient.connect('mongodb://172.16.98.98/hello_world?maxPoolSize=5', function(err, db) {
 MongoClient.connect('mongodb://172.16.98.98/hello_world?maxPoolSize=5', function(err, db) {
   collection = db.collection('world');
   collection = db.collection('world');
 });
 });
@@ -70,6 +75,11 @@ http.createServer(function (req, res) {
   var hello = {message: "Hello, world"};
   var hello = {message: "Hello, world"};
 
 
   var path = url.parse(req.url).pathname;
   var path = url.parse(req.url).pathname;
+  
+  // mysql on windows is not supported
+  if (windows && (path.substr(0, 3) == '/my' || path == '/update')) {
+    path = '/doesntexist';
+  }
 
 
   switch (path) {
   switch (path) {
   case '/json':
   case '/json':
@@ -210,7 +220,7 @@ http.createServer(function (req, res) {
 
 
   default:
   default:
     // File not found handler
     // File not found handler
-    res.writeHead(404, {'Content-Type': 'text/html; charset=UTF-8'});
+    res.writeHead(501, {'Content-Type': 'text/plain; charset=UTF-8'});
     res.end("NOT IMPLEMENTED");
     res.end("NOT IMPLEMENTED");
   }
   }
 }).listen(8080);
 }).listen(8080);

+ 19 - 1
nodejs/setup.py

@@ -9,12 +9,30 @@ def start(args):
   setup_util.replace_text("nodejs/hello.js", "localhost", args.database_host)
   setup_util.replace_text("nodejs/hello.js", "localhost", args.database_host)
 
 
   try:
   try:
-    subprocess.check_call("npm install", shell=True, cwd="nodejs")
+    npm()
     subprocess.Popen("node hello.js", shell=True, cwd="nodejs")
     subprocess.Popen("node hello.js", shell=True, cwd="nodejs")
     return 0
     return 0
   except subprocess.CalledProcessError:
   except subprocess.CalledProcessError:
     return 1
     return 1
+
+def npm():
+  if os.name == 'nt':
+    subprocess.check_call("copy package.json package.json.dist /y > NUL", shell=True, cwd="nodejs")
+    setup_util.replace_text("nodejs/package.json", ".*mysql.*", "")
+    setup_util.replace_text("nodejs/package.json", ".*mapper.*", "")
+  
+  try:
+    subprocess.check_call("npm install", shell=True, cwd="nodejs")
+  finally:
+    if os.name == 'nt':
+      subprocess.check_call("del package.json", shell=True, cwd="nodejs")
+      subprocess.check_call("ren package.json.dist package.json", shell=True, cwd="nodejs")
+
 def stop():
 def stop():
+  if os.name == 'nt':
+    subprocess.Popen("taskkill /f /im node.exe > NUL", shell=True)
+    return 0
+  
   p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
   p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
   out, err = p.communicate()
   out, err = p.communicate()
   for line in out.splitlines():
   for line in out.splitlines():