使用 Node.js 内置 http 模块的createServer()方法创建一个新的HTTP服务器并返回json数据,代码如下:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const data = [
{
name: '测试1号', index: 0 },
{
name: '测试2号', index: 1 },
{
本文介绍了如何使用Node.js的内置http模块创建一个HTTP服务器,处理GET请求返回JSON数据,并实现跨域功能。示例代码展示了如何在`/api/list`路径上提供一个API接口。
使用 Node.js 内置 http 模块的createServer()方法创建一个新的HTTP服务器并返回json数据,代码如下:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const data = [
{
name: '测试1号', index: 0 },
{
name: '测试2号', index: 1 },
{
729
763
4291