场景一:如果只是简单的功能模块,只用到node自带的模块,不用npm init,直接引入自带模块就行
var http = require("http");
var fs = require("fs");
const tableNames = ['GEOPDDISULINE','GEOPDLINE','GEOPDRIUN','GEOPDRIUNLINE','GEOPTCOND','GEOPTLINE','GEOPTLINE_ORIG','GEOPFWELL']
const primaryKeys = ['FGUID','OID','FGUID','FGUID','OID','FGUID','OID','FGUID']
const tableName = tableNames[7]
const primaryKey = primaryKeys[7]
const options = {
hostname: '192.168.1.152',
port: 80,
path: `/dldata/shape/getByTableName?tableName=${tableName}&primaryKey=${primaryKey}`,
method: 'GET'
}
var htmlData = ""
var req = http.request(options, function (res) {
res.on("data", function (chunk) {
htmlData += chunk;
});
res.on("end", function () {
let obj = JSON.parse(htmlData)
console.log(obj);
let targetObj = {}
targetObj.type = "FeatureCollection"
targetObj.features = obj[0].map((item,index)=>{
let o = {}
o.type = 'Feature'
o.properties = {}
let itemKeys = Object.keys(item)
itemKeys.forEach(val=>{
val !='wktstr' && (o.properties[val] = item[val]==null?'':item[val])
if(val == 'wktstr'){
o.geometry = {}
o.geometry.type = 'Multi'+item.wktstr.match(/(.+)\(/)[1].replace(' ','')
let coords = item.wktstr.match(/\((.+)\)/)[1].split(' ')
o.geometry.coordinates = [[+coords[0],+coords[1]],[+coords[0],+coords[1]]]
}
})
return o
})
fs.writeFile(`./${tableName}.txt`, JSON.stringify(targetObj), (err)=>{
console.log(err);
})
});
});
req.end();
场景二: 需要用到第三方模块,要事先npm init初始化项目,初始化成功会出现下面这个文件。
然后需要什么模块就使用npm install进行安装。
const Koa = require('koa')
const app = new Koa()
app.use(async(ctx)=>{
ctx.body = '这是后台返回的数据'
})
app.listen(3000)