亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Node.js 的編碼問題

Node.js 的編碼問題

回首憶惘然 2023-10-14 16:19:30
我是編碼世界的新手,目前正在學校學習,但我似乎遇到了一個我無法解決的問題。我正在嘗試使用終端運行我的 index.js 文件,但我不斷收到此錯誤:Error: Cannot find module 'C:\Users\salij\OneDrive\Desktop\U2A\index'←[90m    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)←[39m←[90m    at Function.Module._load (internal/modules/cjs/loader.js:725:27)←[39m←[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)←[39m←[90m    at internal/main/run_main_module.js:17:47←[39m {  code: ←[32m'MODULE_NOT_FOUND'←[39m,  requireStack: []}我不知道我錯過了什么,因為查看我所提供的步驟,我不應該收到此錯誤。這是我的代碼,以防有人可以幫助我。TIAconst express = require ('express')const app = express ()const formidable = require ('formidable')app.use(express.static (__dirname + "/WebCalculatorSolution/WebCalculator"))app.get('/', (req,res)=>{const word = req.query.word   res.write('<h1>${word}</h1>')   res.end()})const port = 8000app.listen(port, ()=>{    console.log('Server ready at: http://localhost:${port}')})
查看完整描述

4 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

首先,嘗試安裝npm包


npm install express formidable


運行此命令后將安裝所有軟件包


根據您的代碼,您嘗試在用戶通過查詢時顯示單詞。


您必須模板字符串,這樣您就可以直接在字符串中嵌入變量。


你可以這樣做。


const express = require('express')

const app = express()

const formidable = require('formidable')


app.use(express.static(__dirname + "/WebCalculatorSolution/WebCalculator"))


app.get('/', (req, res) => {


  let word;

  word = req.query.word ? req.query.word : "Welcome"; //If word won't passed then show welcome message else show word

  res.write(`<h1>${word}</h1>`)

  res.end()

})


const port = 8000

app.listen(port, () => {

  console.log(`Server ready at: http://localhost:${port}`)

})

不帶查詢參數

https://img1.sycdn.imooc.com/652a5436000102e513930551.jpg

傳遞查詢參數

https://img1.sycdn.imooc.com/652a544a0001be2413970558.jpg


查看完整回答
反對 回復 2023-10-14
?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

使用以下命令安裝軟件包: npm install formidable。然后運行服務器



查看完整回答
反對 回復 2023-10-14
?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

正確的模板文字語法是用反引號 (``) 包裹文本,修改此行:


`Server ready at: http://localhost:${port}`


還有這一行:


`<h1>${word}</h1>`


查看完整回答
反對 回復 2023-10-14
?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

我終于找到了我的問題。在 Visual Studio Code 中運行 cmd 我注意到它試圖從文件資源管理器中完全不同的位置運行我的索引文件。在我將其定向到索引文件實際所在的文件夾后,它終于開始正常工作了。感謝大家的幫助。



查看完整回答
反對 回復 2023-10-14
  • 4 回答
  • 0 關注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號