将JS转换为NodeJS函数

时间:2019-07-17 14:16:46

标签: javascript node.js debugging

0

我是NodeJS的一个新手,我有一个任务要做:将js函数转移到NodeJS。

这是我在HTML中的javascript函数

compressGS1DigitalLink(digitalLinkURI,useShortText,uriStem,uncompressedPrimary,useOptimisations,compressOtherKeyValuePairs) {
            // extract query string
            let firstQuestionMark = digitalLinkURI.indexOf("?");
            let queryString="";
            let nonGS1keyvaluePairs={};
            if (firstQuestionMark > -1) {
                    queryString = digitalLinkURI.substr(1+firstQuestionMark);
            }
            if (queryString !== "") {
                    // if semicolon was used as delimiter between key=value pairs, replace with ampersand as delimiter
                    queryString = queryString.replace(new RegExp(";", 'g'),"&");

                    let firstFragment = queryString.indexOf("#");
                    if (firstFragment > -1) {
                            queryString = queryString.substring(0,firstFragment);
                    }

                    let pairs = queryString.split("&");
                    for (let i=0; i<pairs.length; i++) {
                            let p = pairs[i].split("=");
                            // if the key is not numeric AND is not a shortcode such as exp or expdt, then add to the nonGS1keyvalueePairs
                            if ((p[0] !== null) && (p[1] !== null) && (!(this.regexAllNum.test(p[0])) && (!(this.shortCodeToNumeric.hasOwnProperty(p[0]))))) {
                                    nonGS1keyvaluePairs[p[0]]=this.percentDecode(p[1]);
                            }
                    }
            }

            let gs1AIarray=this.extractFromGS1digitalLink(digitalLinkURI).GS1;
            let compressedDL=this.buildCompressedGS1digitalLink(gs1AIarray,useShortText,uriStem,useOptimisations,compressOtherKeyValuePairs,nonGS1keyvaluePairs);
            return compressedDL;
    }

第二个:

compressedDigitalLinkOutput : function() {
        if (this.elementStringInput !== "") {
            try {
                this.error1="";
                return gs1dlt.gs1ElementStringsToCompressedGS1DigitalLink(this.elementStringInput, (this.shortnames =="true"), this.uristem, (this.uncompressedPrimary=="true") , (this.useOptimisations=="true") );
            } catch(err) {
                this.digitalLinkOutput="";
                this.error1=err+"\n"+err.stack;
                return "";
            }
        } else {
        return "";
        }
    },

我需要做的是将它们都从JS中删除,并将它们放入NodeJS中。

这些功能应该压缩输入:

<tr class="digitalLink"><td class="label">Input: Uncompressed GS1 Digital Link URI</td><td>
<form action="/compression" method="get">
<input id="uncompressedDigitalLinkInput" class="digitalLink" type="text" v-model="uncompressedDigitalLinkInput"></td><td class="charCount">{{uncompressedDigitalLinkInput.length}}</td>
Here is what i tried to do : using get and post request to pass the parameters

  app.post('/compression', function(req,res){
  if (this.uncompressedDigitalLinkInput !== "") {
    try {
      this.error3="";
      return gs1dlt.compressGS1DigitalLink(this.uncompressedDigitalLinkInput,(this.shortnames =="true"),this.uristem,(this.uncompressedPrimary=="true"),(this.useOptimisations=="true"),(this.compressOtherKeyValuePairs=="true"));
    } catch(err) {
      this.error3=err+"\n"+err.stack;
      return "";
    }


  } else {
    return "";
  }
  return uncompressedDigitalLinkInput;
})

假定的输出应为: enter image description here

这是我目前得到的: enter image description here

据我了解,我需要在nodejs函数中传递一些参数,以便它压缩链接并将压缩后的版本还给我,然后我需要将其发送到html。

  var http = require ('http');
var fs = require('fs');
var port = 3000;
var express = require('express');
var path = require('path');
var jsFile = require('./src/GS1DigitalLinkToolkit');
var app = express();

app.use(express.static(path.join(__dirname, 'src')));
app.use('/', jsFile);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});
var server = http.createServer(function(req,res){
  res.writeHead(200,{'Content-Type': 'text/html'})
  fs.readFile('index.html', function(error,data){
    if (error) {
      res.writeHead(404);
      res.write('Error file not found');
    }else {
      res.write(data);
    }
    res.end();
  })
});
server.listen(port,function(error){
  if(error){
    console.log('Something went wrong', error);
  }else {
    console.log("server is listening on port 3000");
  }
})

function compressGS1DigitalLink(digitalLinkURI,useShortText,uriStem,uncompressedPrimary,useOptimisations,compressOtherKeyValuePairs) {
    // extract query string
    let firstQuestionMark = digitalLinkURI.indexOf("?");
    let queryString="";
    let nonGS1keyvaluePairs={};
    if (firstQuestionMark > -1) {
        queryString = digitalLinkURI.substr(1+firstQuestionMark);
    }
    if (queryString !== "") {
        // if semicolon was used as delimiter between key=value pairs, replace with ampersand as delimiter
        queryString = queryString.replace(new RegExp(";", 'g'),"&");

        let firstFragment = queryString.indexOf("#");
        if (firstFragment > -1) {
            queryString = queryString.substring(0,firstFragment);
        }

        let pairs = queryString.split("&");
        for (let i=0; i<pairs.length; i++) {
            let p = pairs[i].split("=");
            // if the key is not numeric AND is not a shortcode such as exp or expdt, then add to the nonGS1keyvalueePairs
            if ((p[0] !== null) && (p[1] !== null) && (!(this.regexAllNum.test(p[0])) && (!(this.shortCodeToNumeric.hasOwnProperty(p[0]))))) {
                nonGS1keyvaluePairs[p[0]]=this.percentDecode(p[1]);
            }
        }
    }

    let gs1AIarray=this.extractFromGS1digitalLink(digitalLinkURI).GS1;
    let compressedDL=this.buildCompressedGS1digitalLink(gs1AIarray,useShortText,uriStem,useOptimisations,compressOtherKeyValuePairs,nonGS1keyvaluePairs);
    return compressedDL;
}

app.post('/compression', function(req,res){
 if (this.uncompressedDigitalLinkInput !== "") {
   try {
     this.error3="";
     const response = gs1dlt.compressGS1DigitalLink(this.uncompressedDigitalLinkInput,(this.shortnames =="true"),this.uristem,(this.uncompressedPrimary=="true"),(this.useOptimisations=="true"),(this.compressOtherKeyValuePairs=="true"));
     res.status(200).send(response);
   } catch(err) {
     this.error3=err+"\n"+err.stack;
     res.status(200).send("");
   }


 } else {
   res.status(200).send("");
 }
 res.status(200).send(uncompressedDigitalLinkInput);
})

module.exports = app;

我不明白为什么它不起作用,通过NodeJS函数,我传递了参数并返回了适当的变量。那我该怎么办?

谢谢

0 个答案:

没有答案