【问题标题】:Issue in running a client js file in Node js express在 Node js express 中运行客户端 js 文件的问题
【发布时间】:2021-04-04 13:33:26
【问题描述】:

我在 node js express 中遇到了一个问题[因为我是 node js 的新手]。我的用例是我有一个三个目录[根目录、路由、html 文件]。在根目录中,我有 index.js 文件,其中包含端口信息,在路由目录中,有一个路由文件,用于提及 API 的所有路由。在 html 文件中,我有静态 html 文件 + client.js 文件。当我转到 /api/getlist 路线时,我正在发送一个 html 文件。在那个 html 文件中,点击一个按钮,我正在尝试运行一个 client.js 文件来进行一些验证..但是控制台显示

getlist:30 GET http://localhost:5000/api/client.js net::ERR_ABORTED 404(未找到)

索引.js

const express = require('express')
> const dotenv = require("dotenv").config(); 
>const port =require('debug')('log:port') 
>const get =require('debug')('log:getroute') 
>const routes = require('./routes/routes') 
>const app = express() 
>const bodyParser =require('body-parser')
> 
> 
> // app.use(express.static('/Users/gowthamkishore/Desktop/folder/node
> js/project'))
> 
> 
> app.use('/api',routes) app.use(express.static('htmlfiles'))
> app.use(bodyParser.urlencoded({ extended: false }))
> app.use(bodyParser.json()); 
> 
> 
> app.listen(process.env.PORT,(req,res)=>{   
> port(process.env.PORT,'Port information') })

 routes.js

>  const express = require('express') 
>const router = express.Router() 
>const path = require('path') 
>const bodyParser = require('body-parser')
> const fs = require('fs')
> 
> router.use(bodyParser.urlencoded({ extended: false }))
> router.use(bodyParser.json());  router.use('/client',
> express.static(path.join(path.resolve(__dirname,'../'),'/htmlfiles/client.js')));
> 
> router.get('/getList/:id',(req,res)=>{
>     console.log(req.params)
>     res.sendStatus(200) })
> 
> 
> 
> router.get('/getlist',(req,res)=>{
>     console.log(path.join(path.resolve(__dirname,'../'),'/htmlfiles/client.js'))
>     console.log(path.join(path.resolve(__dirname,'../'),'/htmlfiles/firstpage'),'Logger')
>     res.sendFile(path.join(path.resolve(__dirname,'../'),'/htmlfiles/firstpage.html'))
>     console.log('In get List')
> 
> })
> 
> router.post('/newlist',async (req,res)=>{
>     res.send('ys') console.log(req.body) res.send(req.body.fname) })
> 
> 
> module.exports = router


firstpage.html

 ```<!DOCTYPE html>
<html>
  <head>
   
  </head>
<body>

<h1>My First Heading</h1>
<a href="https://www.w3schools.com/">Visit W3Schools.com!</a>


<form action="http://localhost:5000/api/newlist" method="POST">
    <label for="fname">First name:</label><br>
    <input type="text" id="fname" name="fname" value="Gowtham"><br>
    <label for="lname">Last name:</label><br>
    <input type="text" id="lname" name="lname" value="Kishire"><br><br>
    <input type="submit" value="Submit">
  </form>

  <h1>Node + Express + MongoDb example</h1>
  <p id="counter">Loading button click data.</p>
  <button id="myButton">Click me!</button>
<p>My first paragraph.</p>


<button id = 'information'>Click here</button>


</body>
<script src="client.js"></script>
</html>
```

Client.js
```
console.log('Client-side code running');

const button = document.getElementById('myButton');
button.addEventListener('click', function(e) {
  console.log('button was clicked');
});```

Please help me on this case

【问题讨论】:

    标签: javascript html node.js express


    【解决方案1】:

    您可以使用date-fns 包。它是tree-shakeable,因此只有您导入的内容才会进入最终捆绑包。您可以使用名为differenceInHours() 的函数。

    包:https://www.npmjs.com/package/date-fns

    differenceInHours:https://date-fns.org/v2.19.0/docs/differenceInHours

    一个参数是数据库中的日期,第二个参数是当前日期。对于当前日期,您只需发送Date.now()

    【讨论】:

    • 我很高兴它对你有用。如果对您有帮助,还可以考虑投票并接受我的回答。
    猜你喜欢
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多