使用nodejs给页面返回数据

在页面发送请求,然后打印,发现没办法给ajax返回值,必须写res.end(JSON.stringify(数据))页面才可以接收到,但是我写一个单纯的字符串也接收不到,必须使用JSON.stringify()这样写才能打印出来

node部分

```html
const http = require('http')
const fs = require('fs')
let statics = require('./static')
let upload = require('./upload')
http.createServer((req, res) => {
    if (req.url == '/favicon.ico') {
        return
    }
    let myurl = new URL(req.url, 'http://localhost:3000')
    console.log(myurl.pathname)
    if (myurl.pathname == '/upload') {
        upload(req, res)
    } else if (myurl.pathname == '/show') {
        fs.readdir('./data', function (err, data) {
            let response = {
                msg: 'success',
                data: data
            }
            res.end(JSON.stringify(response))
        })
    } else {
        statics.static(req, res)
    }
}).listen(3000, 'localhost')



```html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<h1>show</h1>
</body>
<script src="jquery.3.5.1.min.js"></script>
<script>
    $.ajax({
        url: "http://localhost:3000/show",
        type: 'get',
        dataType: 'json',
        success: function(res){
            console.log(res)
        }   
    })
</script>

</html>