需要使用express的节点在静态和动态内容之间切换

时间:2014-06-20 23:22:48

标签: node.js express

问题:REWRITE 了解更多信息后,我正在修改问题。

下面我在“/form.html”上有一个表单 (目前正在做而不是发帖;不要认为这是问题)

表单提交“/ transfer”,它运行一些java代码。我需要将java代码“成功”或“错误”返回给用户。我试过了两个

response.send('success') 

response.writeHead(200, { 'Content-Type': 'text/html' });  res.end('success', 'utf-8');

既不工作


 

    var express = require('express');
    var routes = require('routes');
    var log4js = require('log4js');     // npm install log4js
    var fs = require('fs');
    var http = require('http');
    var java = require("java");
    var path = require("path");
    var upload = require("blueimp-file-upload-node");
    var app = express();

    app.get('/form', function(req, res) {
     fs.readFile('./form.html', function(error, content) {
        if (error) {
            console.log(error);
            res.writeHead(200, { 'Content-Type': 'text/html' });
            res.end('oops', 'utf-8');
        }
        else {
            res.writeHead(200, { 'Content-Type': 'text/html' });
            res.end(content, 'utf-8');
        }
      });
    });


    app.get('/transfer', function(request, response) {

    java.classpath.push("../csv2rdf");
    java.classpath.push("../lib/commons-io-2.4.jar");
    java.classpath.push("../lib/log4j-1.2.16.jar");
    java.classpath.push("../lib/slf4j-api-1.7.5.jar");
    ...
    var result = "";

    try {
      var appx = java.callStaticMethodSync("com.appx.converter.cv2appx.Runner", "nodejs",
      request.query.user,  request.query.pass, request.query.file, request.query.auth);
      result = request.query.user.concat(":", request.query.file);
      console.log(result);  

** how to send success in response ** 

    } catch (ex) {
      result = result.concat(" ", request.query.user, ":", request.query.file);
      console.log(result);

** how to send errors in response **  
     }

     });

    app.listen(3000);



0 个答案:

没有答案