首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >[HTML][Express]在终端中使用npm start时应用程序崩溃

[HTML][Express]在终端中使用npm start时应用程序崩溃
EN

Stack Overflow用户
提问于 2021-10-18 18:25:15
回答 2查看 85关注 0票数 0

下面是我的代码和我看过的类似问题的package.json,但给出的错误代码与我的不同。我能做些什么来防止崩溃和让应用程序运行?

应该打开一个带有主页的本地网站,该主页允许导航到另一个网站,该网站有一个列出了一些股票和价格的表格。

package.json

代码语言:javascript
复制
  "name": "assignment3",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "nodemon": "^2.0.12"
  }
}

我的代码

代码语言:javascript
复制
const PORT = 3000;

// The variable stocks has the same value as the variable stocks in the file 'stocks.js'
const stocks = require('./stocks.js').stocks;

const express = require("express");
const app = express();


app.use(express.urlencoded({
    extended: true
}));

app.use(express.static('public'));
// Note: Don't add or change anything above this line.

// Add your code here

app.get('/',function(req,res) {
    res.sendFile(path.join(__dirname+'/index.html'));
    });

app.get('/listing', function(req, res){
    res.sendFile(path.join(__dirname+ '/stocklist.html'))
     })

app.get('/sort', function(req, res){
    res.sendFile(path.join(__dirname+ '/listingorder.html'))
     })

app.post('/placeorder', (req, res) => {
    console.log(req.body)
res.status(200).send("Placed")
})

app.get('/asc', function(req, res){
    let sorted = stocks.sort(function(a, b) {
        return parseFloat(a.price) - parseFloat(b.price);
    });
    res.status(200).send({stock: sorted})
 })

app.get('/dsc', function(req, res){
   let sorted = stocks.sort(function(a, b) {
       return parseFloat(b.price) - parseFloat(a.price);
   });
   res.status(200).send({stock: sorted})
})

app.get('/stocks', function(req, res) {
    //var data = {stock: stocks};
    res.status(200).send({stock: stocks});
})
app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}...`);
});


index.html

<!DOCTYPE html>

    <><head>

    </head><body>
            <p>Home Page</p>
            <a href="http://localhost:3000/listing">Stocks</a>

            <a href="http://localhost:3000/sort">Order by low high</a>

            <script>

            </script>

        </body><html>

            stocklist.html

            <html>


                <head>

                </head>

                <body>
                    <div>
                    </div>
                    <script>
                        function load(){fetch('http://localhost:3000/stocks')
                            .then(response => response.json())
                            .then(
                                (data) => {
                                    console.log(data);
                                    generate_table(data);
                                }
                            )};
                        }


                        function generate_table(data) {
                            // get the reference for the body
                        }
// get the reference for the body
                        var body = document.getElementsByTagName("div")[0];

// creates a <table> element and a <tbody> element
                            var tbl = document.createElement("table");
                            var tblBody = document.createElement("tbody");
                            var row = document.createElement("tr");
                            var cell = document.createElement("th");
                            var cellText = document.createTextNode("Company");
                            cell.appendChild(cellText);
                            row.appendChild(cell);
                            var cell = document.createElement("th");
                            var cellText = document.createTextNode("Price");
                            cell.appendChild(cellText);
                            row.appendChild(cell);
                            var cell = document.createElement("th");
                            var cellText = document.createTextNode("Symbol");
                            cell.appendChild(cellText);
                            row.appendChild(cell);
                            tblBody.appendChild(row);
                            // creating all cells
                            for (var i = 0; i <data.stock.length />; i++) {
                                // creates a table row
                            }
// creates a table row
                            var row = document.createElement("tr");


                            var cell = document.createElement("td");
                            var cellText = document.createTextNode(data.stock[i].company);
                            cell.appendChild(cellText);
                            row.appendChild(cell);

                            var cell = document.createElement("td");
                            var cellText = document.createTextNode(data.stock[i].price);
                            cell.appendChild(cellText);
                            row.appendChild(cell);

                            var cell = document.createElement("td");
                            var cellText = document.createTextNode(data.stock[i].symbol);
                            cell.appendChild(cellText);
                            row.appendChild(cell);

                            // add the row to the end of the table body
                            tblBody.appendChild(row);
                            }

// put the <tbody> in the <table>
                                tbl.appendChild(tblBody);
// appends <table> into <body>
                                    body.appendChild(tbl);
                                    // sets the border attribute of tbl to 2;
                                    tbl.setAttribute("border", "2");
                                    }


                                    load();

                                    function postForm(ev){ev.preventDefault()};
                                    let body = {symbol}: document.getElementById("symbol").value,
                                    qty: document.getElementById("qty").value,
                                    }
                                    console.log(body);
                                    fetch("http://localhost:3000/placeorder", {
                                        // Adding method type
                                        method}: "POST",
                                    body:body,

                                    })

                                    .then(response => response.json())

                                    .then(json => console.log(json));
                                    }
                                </script>
                                    <br />
                                    <form onsubmit="postForm($event)">
                                        Symbol: <input type="text" id="symbol"></>
                                    </form>

                                    <br />
                                    <br />

                                </body>


                            </html>

                                listingorder.html

                                <html>


                                    <head>

                                    </head>

                                    <body>
                                        <div>
                                        </div>
                                        <script>
                                            function load(){fetch('http://localhost:3000/stocks')
                                                .then(response => response.json())
                                                .then(
                                                    (data) => {
                                                        console.log(data);
                                                        generate_table(data);
                                                    }
                                                )};
                                            }


                                            function generate_table(data) {
                                                // get the reference for the body
                                            }
// get the reference for the body
                                            var body = document.getElementsByTagName("div")[0];

// creates a <table> element and a <tbody> element
                                                var tbl = document.createElement("table");
                                                var tblBody = document.createElement("tbody");
                                                var row = document.createElement("tr");
                                                var cell = document.createElement("th");
                                                var cellText = document.createTextNode("Company");
                                                cell.appendChild(cellText);
                                                row.appendChild(cell);
                                                var cell = document.createElement("th");
                                                var cellText = document.createTextNode("Price");
                                                cell.appendChild(cellText);
                                                row.appendChild(cell);
                                                var cell = document.createElement("th");
                                                var cellText = document.createTextNode("Symbol");
                                                cell.appendChild(cellText);
                                                row.appendChild(cell);
                                                tblBody.appendChild(row);
                                                // creating all cells
                                                for (var i = 0; i <data.stock.length />; i++) {
                                                    // creates a table row
                                                }
// creates a table row
                                                var row = document.createElement("tr");


                                                var cell = document.createElement("td");
                                                var cellText = document.createTextNode(data.stock[i].company);
                                                cell.appendChild(cellText);
                                                row.appendChild(cell);

                                                var cell = document.createElement("td");
                                                var cellText = document.createTextNode(data.stock[i].price);
                                                cell.appendChild(cellText);
                                                row.appendChild(cell);

                                                var cell = document.createElement("td");
                                                var cellText = document.createTextNode(data.stock[i].symbol);
                                                cell.appendChild(cellText);
                                                row.appendChild(cell);

                                                // add the row to the end of the table body
                                                tblBody.appendChild(row);
                                                }

// put the <tbody> in the <table>
                                                    tbl.appendChild(tblBody);
// appends <table> into <body>
                                                        body.appendChild(tbl);
                                                        // sets the border attribute of tbl to 2;
                                                        tbl.setAttribute("border", "2");
                                                        }


                                                        load();

                                                        function postForm(ev){ev.preventDefault()};
                                                        let body = {symbol}: document.getElementById("symbol").value,
                                                        qty: document.getElementById("qty").value,
                                                        }
                                                        console.log(body);
                                                        fetch("http://localhost:3000/placeorder", {
                                                            // Adding method type
                                                            method}: "POST",
                                                        body:body,

                                                        })

                                                        .then(response => response.json())

                                                        .then(json => console.log(json));
                                                        }
                                                    </script>
                                                        <br />
                                                        <form onsubmit="postForm($event)">
                                                            Symbol: <input type="text" id="symbol"></>
                                                        </form>

                                                        <br />
                                                        <br />

                                                    </body>
function StocksByPrice(){
    if (Number(x.table) > Number(y.table)) {
        shouldSwitch = true;
        break;
}
}
                                                </html>


// Note: Don't add or change anything below this line.
app.listen(PORT, () => {console.log(`Server listening on port ${PORT}...`)};
});

运行npm start时,我收到以下错误

代码语言:javascript
复制
> assignment3@1.0.0 start D:\Comp Sci\CS 290 Web Development\Assignment 3
> nodemon server.js

[nodemon] 2.0.13
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
D:\Comp Sci\CS 290 Web Development\Assignment 3\server.js:63
<!DOCTYPE html>
          ^^^^
SyntaxError: Unexpected identifier
    at wrapSafe (internal/modules/cjs/loader.js:988:16)
    at Module._compile (internal/modules/cjs/loader.js:1036:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...
EN

Stack Overflow用户

发布于 2021-10-18 19:05:03

我会尝试创建一个“view”文件夹,并从那里渲染页面。你试过了吗?

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69620815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档