如何在Express.js中包含用于服务器端计算的常规JavaScript脚本/库

我需要计算两个用户之间的距离,通过Geolocation API捕获。 我find了用vanilla JS编写的脚本,它将两个用户的Lat / Long对并返回它们之间的距离:

var distance = distVincenty(lat1, lon1, lat2, lon2); 

请滚动到脚本源页面的底部: http : //www.movable-type.co.uk/scripts/latlong-vincenty.html

我已经尝试使用require('./lib/vincenty'); ,但是每次我打电话distVincenty()它返回distVincenty is not defined

那么我怎么包括/需要vincenty.js到Express束?

阅读“了解Node.js”需求“ 。

发生了什么事情是因为你需要的文件并不意味着你得到了所有的function。 你得到的是文件在require调用的返回值上添加到特殊的exports对象上的函数。