javascript - Can't load scripts because of node.js -
i can't use <script src="node_modules/jquery/dist/jquery.min.js"></script>
in index.html file because of:
failed load resource: server responded status of 404 (not found) http://localhost:8080/node_modules/jquery/dist/jquery.min.js
this happens because of else statement in server file code:
var http = require('http') var url = require('url') var fs = require('fs') var server = http.createserver((req, res) => { let parsedurl = url.parse(req.url, true) if (parsedurl.pathname === '/') { console.log('home page') fs.readfile('./index.html', (err, data) => { res.writehead(200, { 'content-type': 'text/html' }) res.end(data) }) } else if (parsedurl.pathname === '/readjson') { console.log('read json') fs.readfile('./data.json', (err, data) => { res.writehead(200, { 'content-type': 'application/json' }) res.end(data) }) } else { console.log('we can\'nt load resources because of statement') res.writehead(404) res.end() } }) server.listen(8080)
i've read how fix problem when using express module. there way solve problem without using module?
the easiest way load jquery cdn instead of serving own server. accepted best practice.
example:
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgfzhoseeamdoygbf13fyquitwlaqgxvsngt4=" crossorigin="anonymous"></script>
you can find various options loading jquery cdn here: https://code.jquery.com
if create http server in code example , want serve jquery, you'd have read jquery.min.js
using fs.readfile
, serve contents, you're doing data.json
file.
Comments
Post a Comment