0

I am working on a web application and I come to this calculation method that is going to take a string and returns the number of SMS messages.(It's a pure function and doesn't need any external resources to do its job)

I need this function to work both in client-side and server-side.While in client-side user can see the number of SMSs of his message so far and when in server-side i have to calculate how much does the message cost.

I thought that I had to have a C# version of this function for server-side and a javascript one for client-side but then I thought DRY - what if I could run javascript functions in server-side too ? I am not sure if it's possible or not ?

So here's the question : can we run a javascript function in server-side (or in my case from a class library)?

Beatles1692
  • 5,214
  • 34
  • 65

2 Answers2

0

I think you're looking at this from the wrong way. Instead of trying to run javascript server side (which is not possible I think), you should create a C# function to calculate whatever it is you want calculated and pass the result to your client. This function can be invoked by the client through e.g. an ajax call, or passed in through a viewmodel etc.

Hintham
  • 1,078
  • 10
  • 29
0

Solution: 1. You should write API in C# and since it is pure method to calculate, its number of parameters are fixed. Now Client side code can make ajax (or xmlHttpRequest) to this API and response to which can be handled at client side. 2. Use Node for server side javascript something like, https://www.npmjs.com/package/sandbox, or use native node js VM module.

theAnubhav
  • 535
  • 6
  • 16